在Pandas

时间:2017-04-19 13:35:13

标签: pandas dataframe

如果值与标题相同,我想删除数据框中的描述:

m[m.Description == m.Caption].Description = \
    m[m.Description == m.Caption].Description.map(lambda x: '')

我觉得这篇文章非常模糊:

df[condition][columns] = df[condition][columns].map(lambda x: value)

有没有更好的语法来做同样的事情?我想象的是:

df[condition][columns].map(lambda x: value, inplace=True)

1 个答案:

答案 0 :(得分:2)

loc需要boolean indexing

m.loc[m.Description == m.Caption, 'Description'] = ' '

样品:

m = pd.DataFrame({'Description':['a','b','f'],
                   'Caption':['a','c',''],
                   'C':[7,8,9]})

print (m)
   C Caption Description
0  7       a           a
1  8       c           b
2  9                   f

m.loc[m.Description == m.Caption, 'Description'] = ' '
print (m)
   C Caption Description
0  7       a            
1  8       c           b
2  9                   f

或者使用mask

m['Description'] = m['Description'].mask(m.Description == m.Caption, ' ')
print (m)
   C Caption Description
0  7       a            
1  8       c           b
2  9                   f