简体   繁体   中英

python - Map values in dataframe columns from dictionary with list as value

I am trying to map values to dataframe columns from a dictionary. Here is the code:

# 
import pandas as pd
region_dict = {'Russia':['Europe','Eastern Europe'], 
               'South Korea':['Asia','Easter Asia'],
               'Iran':['Asia','Southern Asia'], 
               'North Korea':['Asia','Eastern Asia']}
region_dict
#> {'Russia': ['Europe', 'Eastern Europe'],
   'South Korea': ['Asia', 'Eastern Asia'],
   'Iran': ['Asia', 'Southern Asia'],
   'North Korea': ['Asia', 'Eastern Asia']}
    
df = pd.DataFrame({'Country':['Russia', 'Iran', 'South Korea','USA'],
                   'continent':['NaN','NaN','NaN','Americas'],
                   'sub_region':['NaN','NaN','NaN','Northern America']})
df
#>      Country  continent        sub_region
0       Russia         NaN               NaN
1         Iran         NaN               NaN
2  South Korea         NaN               NaN
3          USA    Americas  Northern America

Desired Output:

#>      Country  Continent  Sub_region
0       Russia   Europe     Eastern Europe  
1         Iran   Asia       Southern Asia
2  South Korea   Asia       Eastern Asia
3          USA   NaN        NaN

I tried

df[['continent','sub_region']] = df['country'].map(region_dict)

***Error***
ValueError: shape mismatch: value array of shape (20036,) could not be broadcast to indexing result of shape (2,20036)

This way works when the dictionary is in the form

region_dict = {'Russia':'Asia'}

and I map as

df['continent'] = df['country'].map(region_dict)

Questions

  1. How to map multiple columns from a dictionary having list of multiple items as values?
  2. If I want to add only the sub_region column to the df and want to add region_dict['Russia'][1] to the df column, how shall I do it?

You should convert the mapped series of lists to a df and then assign:

df[['continent','sub_region']] = pd.DataFrame(df['Country'].map(region_dict).tolist())

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