我有一个像这样的DataFrame:
-2.0
1.0
4.0
我的目的是将“确定”和“是”重命名为“确认”,以便DataFrame看起来像:
col1 col 2
abc sure
def yes
ghi no
jkl no
mno sure
pqr yes
stu sure
如何做到这一点:)?
答案 0 :(得分:4)
你可以:
df = df.replace(['yes','sure'],'confirm')
答案 1 :(得分:3)
另一种方法是使用Series.map()
映射'yes'
和'sure'
到'confirm'
和'no'
到'no'
。示例 -
mapping = {'sure':'confirm','yes':'confirm','no':'no'}
df['col2'] = df['col2'].map(mapping)
演示 -
In [67]: df
Out[67]:
col1 col2
0 abc sure
1 def yes
2 ghi no
3 jkl no
4 mno sure
5 pqr yes
6 stu sure
In [68]: mapping = {'sure':'confirm','yes':'confirm','no':'no'}
In [69]: df['col2'] = df['col2'].map(mapping)
In [70]: df
Out[70]:
col1 col2
0 abc confirm
1 def confirm
2 ghi no
3 jkl no
4 mno confirm
5 pqr confirm
6 stu confirm