0

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?

1 Answer 1

1

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())
Sign up to request clarification or add additional context in comments.

3 Comments

Your solution worked. But when I tried to apply it to a larger dataframe that already had 2 columns for continent and sub_region, I got the error : ValueError: Columns must be same length as key. I have added to the question to reflect that scenario.
Even to the original question, when I run pd.DataFrame(df['Country'].map(region_dict).tolist()), I get this error : TypeError: object of type 'float' has no len()
@rahul-ahuja that is because you have non matching members, try assigning pd.DataFrame(df['Country'].map(region_dict).dropna().tolist())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.