【发布时间】:2019-07-13 02:33:27
【问题描述】:
我有 3 个具有共同国家索引的数据框。我需要根据该 Country 字段组合这 3 个中的每一个。
我的第一次尝试是结合两个,然后是第三个,这就是我得到的结果:
pd.merge(energy, GDP, how='outer', left_index=True, right_index=True)
我尝试了 3 个在此网站上获得高度评价的选项:
import functools
dfs = [energy, GDP, ScimEn]
df_final = functools.reduce(lambda left,right: pd.merge(left,right,on='Country'), dfs)
energy.merge(GDP,on='Country').merge(ScimEn,on='Country')
pd.concat([energy.set_index('Country'), GDP.set_index('Country'), ScimEn.set_index('Country')], axis=1)
KeyError: '国家'
在处理上述异常的过程中,又发生了一个异常:
密钥错误
Traceback(最近一次通话最后一次) 在 () 40 #df_final = functools.reduce(lambda left,right: pd.merge(left,right,on='Country'), dfs) 41 #energy.merge(GDP,on='Country').merge(ScimEn,on='Country') ---> 42 pd.concat([energy.set_index('Country'), GDP.set_index('Country'), ScimEn.set_index('Country')], axis=1)
【问题讨论】:
-
您应该始终包含一些数据样本,以便您的错误可以是reproducible
-
pd.concat([x.set_index('Country') for x in dfs], axis=1)? -
Quang: 我得到错误'str' object has no attribute 'set_index'
标签: python-3.x pandas