I have trying to add a column D to below df and add condition like this: if column C is in Shanghai, then D is Asia, if column C is SFA, then D is America...take these two as an example, my code as following:
A B C
0 Joe 23 SFA
1 Amy 40 SFA
2 Jenny 34 SFA
3 Kitty 20 Shanghai
4 David 19 Shanghai
...
code:
df['D'] = np.where(
df['C'] == 'SFA','America',
np.where(df['C'] =='Shnaghai','Asia','Other'
)
)
But it keeps giving an error showing: KeyError:'C' I have no idea why it give me this error always as I am pretty sure the data frame is pandas and the column C is being converted to string. Can anyone provide me any insights?