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