Padans赋值及操作

本文介绍了如何使用Python的pandas库进行DataFrame的基本操作,包括创建、索引访问、修改元素、条件筛选和列操作,如添加新列和数据重塑。通过实例展示了如何灵活运用这些技巧来处理和转换数据。
摘要由CSDN通过智能技术生成
import pandas as pd
import numpy as np
dates = np.arange(20210712, 20210718)
df1 = pd.DataFrame(np.arange(24).reshape(6, 4), index=[dates], columns=['A', 'B', 'C', 'D'])
print(df1)
print()
print(df1.iloc[2, 2])  # 位置的方式
print()
df1.iloc[2, 2]=100
print(df1)
print()
print(df1.loc[20210714, 'B'])
print()
df1[df1.A > 10] = 0
print(df1)
print()
df1.A[df1.A == 0] = 1
print(df1)
print()
df1['E'] = 10  # 添加一列
print(df1)
print()
# df1['F'] = pd.Series([1, 2, 3, 4, 5, 6], index=dates)  # 添加不上
# print(df1)
# df1.loc=[20210718, ['A', 'B', 'C', 'D']]=[1, 2, 3]
s1 = pd.Series([1, 2, 3, 4, 5, 6], index=['A', 'B', 'C', 'D', 'E', 'F'])
s1.name = 'S1'
df2 = df1.append(s1)
print(df2)
print()
df1.insert(1, 'G', df2['E'])  # 在第一列插入索引为G的df2中的E列
print(df1)
print()
g = df1.pop('G')  # 弹出G列
df1.insert(5, 'G', g)  # 在最后插入
print(df1)
print()
del df1['G']  # 删除G列
print(df1)
print()
df3 = df1.drop(['A', 'B'], axis=1)  # 删除AB列
print(df3)
print()
df4 = df1.drop([20210713, 20210716], axis=0)
print(df4)
           A   B   C   D
20210712   0   1   2   3
20210713   4   5   6   7
20210714   8   9  10  11
20210715  12  13  14  15
20210716  16  17  18  19
20210717  20  21  22  23

10

           A   B    C   D
20210712   0   1    2   3
20210713   4   5    6   7
20210714   8   9  100  11
20210715  12  13   14  15
20210716  16  17   18  19
20210717  20  21   22  23

20210714    9
Name: B, dtype: int32

          A  B    C   D
20210712  0  1    2   3
20210713  4  5    6   7
20210714  8  9  100  11
20210715  0  0    0   0
20210716  0  0    0   0
20210717  0  0    0   0

          A  B    C   D
20210712  1  1    2   3
20210713  4  5    6   7
20210714  8  9  100  11
20210715  1  0    0   0
20210716  1  0    0   0
20210717  1  0    0   0

          A  B    C   D   E
20210712  1  1    2   3  10
20210713  4  5    6   7  10
20210714  8  9  100  11  10
20210715  1  0    0   0  10
20210716  1  0    0   0  10
20210717  1  0    0   0  10

             A  B    C   D   E    F
(20210712,)  1  1    2   3  10  NaN
(20210713,)  4  5    6   7  10  NaN
(20210714,)  8  9  100  11  10  NaN
(20210715,)  1  0    0   0  10  NaN
(20210716,)  1  0    0   0  10  NaN
(20210717,)  1  0    0   0  10  NaN
S1           1  2    3   4   5  6.0

          A   G  B    C   D   E
20210712  1  10  1    2   3  10
20210713  4  10  5    6   7  10
20210714  8  10  9  100  11  10
20210715  1  10  0    0   0  10
20210716  1  10  0    0   0  10
20210717  1  10  0    0   0  10

          A  B    C   D   E   G
20210712  1  1    2   3  10  10
20210713  4  5    6   7  10  10
20210714  8  9  100  11  10  10
20210715  1  0    0   0  10  10
20210716  1  0    0   0  10  10
20210717  1  0    0   0  10  10

          A  B    C   D   E
20210712  1  1    2   3  10
20210713  4  5    6   7  10
20210714  8  9  100  11  10
20210715  1  0    0   0  10
20210716  1  0    0   0  10
20210717  1  0    0   0  10

            C   D   E
20210712    2   3  10
20210713    6   7  10
20210714  100  11  10
20210715    0   0  10
20210716    0   0  10
20210717    0   0  10

          A  B    C   D   E
20210712  1  1    2   3  10
20210714  8  9  100  11  10
20210715  1  0    0   0  10
20210717  1  0    0   0  10

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值