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?