我确实有一个像这样的DataFrame:
col1 col 2
abc sure
def yes
ghi no
jkl no
mno sure
pqr yes
stu sure
Run Code Online (Sandbox Code Playgroud)
我的意图是将“ sure”和“ yes”重命名为“ confirm”,以使DataFrame看起来像:
col1 col 2
abc confirm
def confirm
ghi no
jkl no
mno confirm
pqr confirm
stu confirm
Run Code Online (Sandbox Code Playgroud)
这该怎么做 :)?
另一种方法是使用Series.map()映射'yes'和'sure'to'confirm'和'no'to 'no'。例子 -
mapping = {'sure':'confirm','yes':'confirm','no':'no'}
df['col2'] = df['col2'].map(mapping)
Run Code Online (Sandbox Code Playgroud)
演示 -
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
822 次 |
| 最近记录: |