我有一个pandas数据帧。大约有50列和50000行。如果一个列有一行
str.contains('certain response')
我想将其他列响应更改为
'NA'
我该怎么做?
答案 0 :(得分:0)
我认为您需要str.contains
条件,如果需要NaN
替换使用np.nan
:
df.loc[df.column1.str.contains('certain response'), 'column 2'] = np.nan
如果True
获得NaN
s:
mask
df['column 2'] = df['column 2'].mask(df.column1.str.contains('certain response'))
样品:
df = pd.DataFrame({'column1':['certain response','certain response 2','aaa'],
'column 2':[2,3,4]})
df['column 2'] = df['column 2'].mask(df.column1.str.contains('certain response'))
print (df)
column 2 column1
0 NaN certain response
1 NaN certain response 2
2 4.0 aaa