Pandas: Conditionally replace values based on other columns values
Now my goal is for each add_rd in the event column, the associated NaN-value in the environment column should be replaced with a string RD.
As per @Zero's comment, use pd.DataFrame.loc
and Boolean indexing:
df.loc[df['event'].eq('add_rd') & df['environment'].isnull(), 'environment'] = 'RD'
You could consider using where
:
df.environment.where((~df.environment.isnull()) & (df.event != 'add_rd'),
'RD', inplace=True)
If the condition is not met, the values is replaced by the second element.