I have a pandas dataframe. There are about 50 columns and 50000 rows. If one colums has a row that
str.contains('certain response')
I want to change the other column response to
'NA'
How would I do that?
I have a pandas dataframe. There are about 50 columns and 50000 rows. If one colums has a row that
str.contains('certain response')
I want to change the other column response to
'NA'
How would I do that?
I believe you need str.contains
for condition and if need replace by NaN
use np.nan
:
df.loc[df.column1.str.contains('certain response'), 'column 2'] = np.nan
Or use mask
, if True
get NaN
s:
df['column 2'] = df['column 2'].mask(df.column1.str.contains('certain response'))
Sample:
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