I'm trying to format following dataframe (it can be copied to clipboard and read by pd ):
Given the dictionary where for each country
there is a list of alternative names, I am trying to "explode" values with duplicate rows where country
values are updated.
Dictonary to update country_names:
{
'UAE':['United Arab Emirates', 'UAE'],
'Rep of Tunisia': ['Tunisia','Rep of Tunisia']
}
desired result:
It seemed like an easy thing but I stuck, any help appreciated.
CodePudding user response:
You can map
your dictionary, then explode
:
out = (df.assign(country=df['country'].map(country_names))
.explode('country', ignore_index=True)
)
output:
country period
0 United Arab Emirates 2022-01-01
1 UAE 2022-01-01
2 Tunisia 2022-10-01
3 Rep of Tunisia 2022-10-01