Reputation: 1513
I am referencing to this thread. I want to update a column value based on value in another column.
name_list = ['C', 'E']
df = pd.DataFrame([('A', 'buy'),
('B', 'sell'),
('C', 'hold'),
('D', 'loan'),
('E', 'hold')], columns=['name', 'action'])
name action
1 A buy
2 B sell
3 C hold
4 D loan
5 E hold
Here is two tries I came up with:
df['action'] = df.apply(lambda x: 'removed' if x['name'] in name_list else df['action'])
df['action'] = df.apply(lambda x: 'removed' if x['name'].isin(name_list) else df['action'])
Both tries above gives this error KeyError: 'name'
The expected output is
name action
1 A buy
2 B sell
3 C removed
4 D loan
5 E removed
What am I doing wrong?
Upvotes: 1
Views: 110
Reputation: 24304
Simply use loc:
df.loc[df['name'].isin(name_list),'action']='removed'
OR
For you current method pass axis=1
but using apply()
is slow for this simple case:
df['action'] = df.apply(lambda x: 'removed' if x['name'] in name_list else x['action'],axis=1)
output of df
:
name action
0 A buy
1 B sell
2 C removed
3 D loan
4 E removed
Note: you can also use np.where()
:
np.where(df['name'].isin(name_list),'removed',df['action'])
Upvotes: 3
Reputation: 9081
Use df.where-
df['action'] = df['action'].where(~df['name'].isin(name_list), 'removed')
Output
0 buy
1 sell
2 removed
3 loan
4 removed
Name: action, dtype: object
Upvotes: 1