开源堡垒机Jumpserver全网独家视频教程系列第一部上线了!免费章节pan.baidu.com/s/1LQ3c323XpG0is1172REnVg 提取码wp0i 视频详细介绍点我

替换

阅读: 2065     评论:0

可以使用replace将pandas对象中的指定值替换为别的值:

In [77]: df = pd.DataFrame(np.random.randint(12,size=(4,3)))

In [78]: df
Out[78]:
   0  1   2
0  4  5  10
1  3  0   3
2  7  7   4
3  6  4   4

In [79]: df.replace(4, NA)  # 将4替换为缺失值
Out[79]:
     0    1     2
0  NaN  5.0  10.0
1  3.0  0.0   3.0
2  7.0  7.0   NaN
3  6.0  NaN   NaN

In [80]: df.replace([3,4], NA) # 将3和4都替换为缺失值
Out[80]:
     0    1     2
0  NaN  5.0  10.0
1  NaN  0.0   NaN
2  7.0  7.0   NaN
3  6.0  NaN   NaN

In [81]: df.replace([3,4], [NA,0]) # 3和4分别替换为缺失值和0
Out[81]:
     0    1     2
0  0.0  5.0  10.0
1  NaN  0.0   NaN
2  7.0  7.0   0.0
3  6.0  0.0   0.0

In [82]: df.replace({3:NA,4:0})  # 参数的字典形式
Out[82]:
     0    1     2
0  0.0  5.0  10.0
1  NaN  0.0   NaN
2  7.0  7.0   0.0
3  6.0  0.0   0.0

 删除重复值 重命名轴索引 

评论总数: 0


点击登录后方可评论