简体   繁体   中英

Filling NaN in a DataFrame Column with Key from a Dictionary by looking up values from a different column

I have a dataset that looks like:

> Country                     Code
> 'Bolivia'                   NaN
> 'Bolivia, The Republic of'  NaN

And I also have a dictionary

> CountryCode = {'BOL':['Bolivia','Bolivia, The Republic of']}

How do I go on about fillna in the dataframe with the respective Key if one of the values is in the dictionary?

The desired output is

> Country                     Code
> 'Bolivia'                   'BOL'
> 'Bolivia, The Republic of'  'BOL'

Thanks for your help!

Create reverse dictionary of CountryCode and map it with Country column:

new_countrycode = {v:key for key,value in CountryCode.items() for v in value}
df['Code'] = df['Country'].map(new_countrycode)

print(df)
                    Country Code
0                   Bolivia  BOL
1  Bolivia, The Republic of  BOL

print(new_countrycode)
{'Bolivia': 'BOL', 'Bolivia, The Republic of': 'BOL'}

Using .apply()

df["Code"] = df.Country.apply(lambda x: ''.join(i for i, j in CountryCode.items() if x in j))

Output:

                    Country Code
0                   Bolivia  BOL
1  Bolivia, The Republic of  BOL
df=pd.DataFrame({'Country':['Bolivia','Bolivia, The Republic of'],'code':[None,None]})

Create Dataframe from dictionary of key-value code

df_keyval=pd.DataFrame({'CountryCode':{'BOL':['Bolivia','Bolivia, The Republic of']}}).reset_index()

Match the Country and get the corresponding Key:

for idx,rows in df.iterrows():
    if rows['Country'] in df_keyval.CountryCode[0]:
        df['code']=df_keyval.index[0]

Output:

    Country                    code
0   Bolivia                     BOL
1   Bolivia, The Republic of    BOL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM