I have a dataframe as shown below.
df:
id player country_code country
1 messi arg argentina
2 neymar bra brazil
3 tevez arg argentina
4 aguero arg argentina
5 rivaldo bra brazil
6 owen eng england
7 lampard eng england
8 gerrard eng england
9 ronaldo bra brazil
10 marria arg argentina
from the above df, I would like to extract the mapping dictionary that relates the country_code
with country
columns.
Expected Output:
d = {'arg':'argentina', 'bra':'brazil', 'eng':'england'}
CodePudding user response:
Dictionary has unique keys, so is possible convert Series
with duplicated index
by column country_code
:
d = df.set_index('country_code')['country'].to_dict()
If there is possible some country
should be different per country_code
, then is used last value per country
.