2015-10-20 65 views
2

我有這樣一個數據幀:重命名值

col1 col 2 
abc sure 
def yes 
ghi no 
jkl no 
mno sure 
pqr yes 
stu sure 

我的目的是重命名「確定」和「是」進入「確認」,使數據幀的樣子:

col1 col 2 
abc confirm 
def confirm 
ghi no 
jkl no 
mno confirm 
pqr confirm 
stu confirm 

如何做到這一點:)?

回答

4

你可以:

df = df.replace(['yes','sure'],'confirm') 
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