Reputation: 1998
I have a df below as:
col_a | col_b
Day Afternoon
Night NaN
Day Morning
Lunch NaN
Day Morning
Day Early Morning
Day Afternoon
Lunch NaN
Night NaN
I want to replace all values in col_a that are "Day" with the value they are in col_b but leave all other values as they are in col_a
Thanks
Upvotes: 1
Views: 48
Reputation: 1319
You could use .where method:
df = df.where(df != 'Day', df['col_b'], axis=0)
print(df)
From the documentation:
". . .Where cond is True, keep the original value. Where False, replace with corresponding value from other."
Output mini-example:
col_a col_b
0 Afternoon Afternoon
1 Night NaN
2 Morning Morning
3 Lunch Early Morning
Upvotes: 1
Reputation: 26676
m=df.col_a=='Day'#boolean select
df.loc[m,'col_a']=df.loc[m,'col_b']#mask and replace
Upvotes: 2