用列表作为值映射字典中数据框列中的值



我正在尝试将值映射到字典中的数据框列。下面是代码:

# 
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

所需输出:

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

我试着

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)

当字典的格式为

时,这种方法有效。
region_dict = {'Russia':'Asia'}

和I映射为

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

  1. 如何从具有多个项目作为值的列表的字典映射多列?
  2. 如果我只想将sub_region列添加到df中,并且想将region_dict['Russia'][1]添加到df列中,我该怎么做?

您应该将映射的列表系列转换为df,然后赋值:

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

相关内容

  • 没有找到相关文章

最新更新