概述
1、数值的设置
import
numpy as
np
import
pandas as pd
dates = pd.date_range('20130101',periods=6)
df = pd.DataFrame(np.arange(24).reshape(6,4),index=dates,columns=['a','b','c','d'])
print(df)
df.iloc[2,2] = 1000000#第三行第三列上的数值改变
print(df)
df.loc['20130102','c'] = 200000
print(df)
df.a[df.a>4] = 0#将a列中大于4的值赋值为0
print(df)
df['F'] = np.nan#加上第F列,数值为空nan
print(df)
df['e'] = pd.Series([1,2,3,4,5,6],index = pd.date_range('20130101',periods = 6))#加上第e列,数值为123456,注意标签要和原标签一致
print(df)
结果:
a
b
c
d
2013-01-01
0
1
2
3
2013-01-02
4
5
6
7
2013-01-03
8
9
10
11
2013-01-04
12
13
14
15
2013-01-05
16
17
18
19
2013-01-06
20
21
22
23
a
b
c
d
2013-01-01
0
1
2
3
2013-01-02
4
5
6
7
2013-01-03
8
9
1000000
11
2013-01-04
12
13
14
15
2013-01-05
16
17
18
19
2013-01-06
20
21
22
23
a
b
c
d
2013-01-01
0
1
2
3
2013-01-02
4
5
200000
7
2013-01-03
8
9
1000000
11
2013-01-04
12
13
14
15
2013-01-05
16
17
18
19
2013-01-06
20
21
22
23
a
b
c
d
2013-01-01
0
1
2
3
2013-01-02
4
5
200000
7
2013-01-03
0
9
1000000
11
2013-01-04
0
13
14
15
2013-01-05
0
17
18
19
2013-01-06
0
21
22
23
a
b
c
d
F
2013-01-01
0
1
2
3 NaN
2013-01-02
4
5
200000
7 NaN
2013-01-03
0
9
1000000
11 NaN
2013-01-04
0
13
14
15 NaN
2013-01-05
0
17
18
19 NaN
2013-01-06
0
21
22
23 NaN
a
b
c
d
F
e
2013-01-01
0
1
2
3 NaN
1
2013-01-02
4
5
200000
7 NaN
2
2013-01-03
0
9
1000000
11 NaN
3
2013-01-04
0
13
14
15 NaN
4
2013-01-05
0
17
18
19 NaN
5
2013-01-06
0
21
22
23 NaN
6
Process finished with exit code 0
2、处理没有数据的数模块nan
import
numpy as
np
import
pandas as pd
dates = pd.date_range('20130101',periods=6)
df = pd.DataFrame(np.arange(24).reshape(6,4),index=dates,columns=['a','b','c','d'])
print(df)
df.iloc[0,1] = np.nan
df.iloc[1,2] = np.nan
print(df)
print(df.dropna(axis = 0,how='any'))#how = any或者all,其中,any的情况是有nan的行丢掉,all的情况是该行所以的值是nan时才会把该行丢掉
df.iloc[0,1] = np.nan
print(df.fillna(value = 0))#给nan填为0
df.iloc[0,1] = np.nan
print(df.isna())#判断是否为nan
print(np.any(df.isna())==True)#判断是否有nan,有的话为true
结果:
a
b
c
d
2013-01-01
0
1
2
3
2013-01-02
4
5
6
7
2013-01-03
8
9
10
11
2013-01-04
12
13
14
15
2013-01-05
16
17
18
19
2013-01-06
20
21
22
23
a
b
c
d
2013-01-01
0
NaN
2.0
3
2013-01-02
4
5.0
NaN
7
2013-01-03
8
9.0
10.0
11
2013-01-04
12
13.0
14.0
15
2013-01-05
16
17.0
18.0
19
2013-01-06
20
21.0
22.0
23
a
b
c
d
2013-01-03
8
9.0
10.0
11
2013-01-04
12
13.0
14.0
15
2013-01-05
16
17.0
18.0
19
2013-01-06
20
21.0
22.0
23
a
b
c
d
2013-01-01
0
0.0
2.0
3
2013-01-02
4
5.0
0.0
7
2013-01-03
8
9.0
10.0
11
2013-01-04
12
13.0
14.0
15
2013-01-05
16
17.0
18.0
19
2013-01-06
20
21.0
22.0
23
a
b
c
d
2013-01-01
False
True
False
False
2013-01-02
False
False
True
False
2013-01-03
False
False
False
False
2013-01-04
False
False
False
False
2013-01-05
False
False
False
False
2013-01-06
False
False
False
False
True
Process finished with exit code 0
最后
以上就是淡淡机器猫为你收集整理的Python学习之pandas(二)的全部内容,希望文章能够帮你解决Python学习之pandas(二)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复