1,pandas操作主要有对指定位置的赋值,如上一篇中的数据选择一样,根据loc,iloc,ix选择指定位置,直接赋值
2,插入,insert方法,插入行和列
3,添加
4,删除 drop方法
5,弹出 pop方法
In [1]:
import pandas as pd
import numpy as np
In [53]:
dates = np.arange(20190809,20190815)
df1 = pd.DataFrame(np.arange(24).reshape(6,4),index=dates,columns=["A","B","C","D"])
df1
Out[53]:
A
B
C
D
20190809
0
1
2
3
20190810
4
5
6
7
20190811
8
9
10
11
20190812
12
13
14
15
20190813
16
17
18
19
20190814
20
21
22
23
In [20]:
df1.iloc[2,2]
Out[20]:
10
In [44]:
df1.iloc[2,2] = 100
df1
Out[44]:
A
B
C
D
20190809
0
1
2
3
20190810
4
5
6
7
20190811
8
9
100
11
20190812
12
13
14
15
20190813
16
17
18
19
20190814
20
21
22
23
In [40]:
df1.loc[20190810,"B"]=200
df1
Out[40]:
A
B
C
D
20190809
0
1
2
3
20190810
4
200
6
7
20190811
8
9
10
11
20190812
12
13
14
15
20190813
16
17
18
19
20190814
20
21
22
23
In [54]:
df1[df1.A>10]=0
df1
Out[54]:
A
B
C
D
20190809
0
1
2
3
20190810
4
5
6
7
20190811
8
9
10
11
20190812
0
0
0
0
20190813
0
0
0
0
20190814
0
0
0
0
In [55]:
df1.A[df1.A==0]=100
df1
Out[55]:
A
B
C
D
20190809
100
1
2
3
20190810
4
5
6
7
20190811
8
9
10
11
20190812
100
0
0
0
20190813
100
0
0
0
20190814
100
0
0
0
In [56]:
#插入一列
df1["E"]=10
df1
Out[56]:
A
B
C
D
E
20190809
100
1
2
3
10
20190810
4
5
6
7
10
20190811
8
9
10
11
10
20190812
100
0
0
0
10
20190813
100
0
0
0
10
20190814
100
0
0
0
10
In [59]:
df1["F"]=pd.Series([1,2,3,4,5,6],index=dates)
df1
Out[59]:
A
B
C
D
E
F
20190809
100
1
2
3
10
1
20190810
4
5
6
7
10
2
20190811
8
9
10
11
10
3
20190812
100
0
0
0
10
4
20190813
100
0
0
0
10
5
20190814
100
0
0
0
10
6
In [62]:
#添加一行
df1.loc[20190815,["A","B","C"]]=[5,6,8]
df1
Out[62]:
A
B
C
D
E
F
20190809
100.0
1.0
2.0
3.0
10.0
1.0
20190810
4.0
5.0
6.0
7.0
10.0
2.0
20190811
8.0
9.0
10.0
11.0
10.0
3.0
20190812
100.0
0.0
0.0
0.0
10.0
4.0
20190813
100.0
0.0
0.0
0.0
10.0
5.0
20190814
100.0
0.0
0.0
0.0
10.0
6.0
20190815
5.0
6.0
8.0
NaN
NaN
NaN
In [65]:
s1=pd.Series([1,2,3,4,5,6],index=["A","B","C","D","E","F"])
s1.name="S1"
df2 = df1.append(s1)
df2
Out[65]:
A
B
C
D
E
F
20190809
100.0
1.0
2.0
3.0
10.0
1.0
20190810
4.0
5.0
6.0
7.0
10.0
2.0
20190811
8.0
9.0
10.0
11.0
10.0
3.0
20190812
100.0
0.0
0.0
0.0
10.0
4.0
20190813
100.0
0.0
0.0
0.0
10.0
5.0
20190814
100.0
0.0
0.0
0.0
10.0
6.0
20190815
5.0
6.0
8.0
NaN
NaN
NaN
S1
1.0
2.0
3.0
4.0
5.0
6.0
In [67]:
#插入一列
df1.insert(1,"G",df2["E"])
df1
Out[67]:
A
G
B
C
D
E
F
20190809
100.0
10.0
1.0
2.0
3.0
10.0
1.0
20190810
4.0
10.0
5.0
6.0
7.0
10.0
2.0
20190811
8.0
10.0
9.0
10.0
11.0
10.0
3.0
20190812
100.0
10.0
0.0
0.0
0.0
10.0
4.0
20190813
100.0
10.0
0.0
0.0
0.0
10.0
5.0
20190814
100.0
10.0
0.0
0.0
0.0
10.0
6.0
20190815
5.0
NaN
6.0
8.0
NaN
NaN
NaN
In [68]:
g=df1.pop("G")
df1.insert(6,"G",g)
df1
Out[68]:
A
B
C
D
E
F
G
20190809
100.0
1.0
2.0
3.0
10.0
1.0
10.0
20190810
4.0
5.0
6.0
7.0
10.0
2.0
10.0
20190811
8.0
9.0
10.0
11.0
10.0
3.0
10.0
20190812
100.0
0.0
0.0
0.0
10.0
4.0
10.0
20190813
100.0
0.0
0.0
0.0
10.0
5.0
10.0
20190814
100.0
0.0
0.0
0.0
10.0
6.0
10.0
20190815
5.0
6.0
8.0
NaN
NaN
NaN
NaN
In [69]:
#删除列
del df1["G"]
df1
Out[69]:
A
B
C
D
E
F
20190809
100.0
1.0
2.0
3.0
10.0
1.0
20190810
4.0
5.0
6.0
7.0
10.0
2.0
20190811
8.0
9.0
10.0
11.0
10.0
3.0
20190812
100.0
0.0
0.0
0.0
10.0
4.0
20190813
100.0
0.0
0.0
0.0
10.0
5.0
20190814
100.0
0.0
0.0
0.0
10.0
6.0
20190815
5.0
6.0
8.0
NaN
NaN
NaN
In [70]:
df2 = df1.drop(["A","B"],axis=1)
df1
Out[70]:
A
B
C
D
E
F
20190809
100.0
1.0
2.0
3.0
10.0
1.0
20190810
4.0
5.0
6.0
7.0
10.0
2.0
20190811
8.0
9.0
10.0
11.0
10.0
3.0
20190812
100.0
0.0
0.0
0.0
10.0
4.0
20190813
100.0
0.0
0.0
0.0
10.0
5.0
20190814
100.0
0.0
0.0
0.0
10.0
6.0
20190815
5.0
6.0
8.0
NaN
NaN
NaN
In [71]:
df2
Out[71]:
C
D
E
F
20190809
2.0
3.0
10.0
1.0
20190810
6.0
7.0
10.0
2.0
20190811
10.0
11.0
10.0
3.0
20190812
0.0
0.0
10.0
4.0
20190813
0.0
0.0
10.0
5.0
20190814
0.0
0.0
10.0
6.0
20190815
8.0
NaN
NaN
NaN
In [73]:
#删除行
df2=df1.drop([20190810,20190812],axis=0)
df1
Out[73]:
A
B
C
D
E
F
20190809
100.0
1.0
2.0
3.0
10.0
1.0
20190810
4.0
5.0
6.0
7.0
10.0
2.0
20190811
8.0
9.0
10.0
11.0
10.0
3.0
20190812
100.0
0.0
0.0
0.0
10.0
4.0
20190813
100.0
0.0
0.0
0.0
10.0
5.0
20190814
100.0
0.0
0.0
0.0
10.0
6.0
20190815
5.0
6.0
8.0
NaN
NaN
NaN
In [74]:
df2
Out[74]:
A
B
C
D
E
F
20190809
100.0
1.0
2.0
3.0
10.0
1.0
20190811
8.0
9.0
10.0
11.0
10.0
3.0
20190813
100.0
0.0
0.0
0.0
10.0
5.0
20190814
100.0
0.0
0.0
0.0
10.0
6.0
20190815
5.0
6.0
8.0
NaN
NaN
NaN