i have a dictionary and a df column contains the country code “BHR”,”SAU”,”ARE”..etc
how to update this column so if it find any of the dict keys it will create new column [“TIMEZONE”] row to the dict value. also add if statement that if the row is not equal to the key add a default value
JavaScript
x
5
1
TimeZoneTableInplace={'BHR':'(GMT+03:00) Baghdad, Kuwait, Riyadh',
2
'SAU':'(GMT+03:00) Baghdad, Kuwait, Riyadh',
3
'ARE':'(GMT+04:00) Abu Dhabi, Muscat'}
4
5
here is my try but it is only updating the last key in the dictionary.
JavaScript
1
4
1
for i,x in TimeZoneTableInplace.items():
2
3
df['TIMEZONE']=np.where(df['COUNTRY']==i,x,'(GMT+03:00) Baghdad, Kuwait, Riyadh'))
4
Advertisement
Answer
This is map
:
JavaScript
1
4
1
df['TIMEZONE'] = (df['COUNTRY'].map(TimeZoneTableInplace)
2
.fillna('(GMT+03:00) Baghdad, Kuwait, Riyadh')
3
)
4