我的分析中有多个数据框。例如数据框1,其中这是中国活动的人数
Activity No of people
Activity 1 100
Activity 2 200
Activity 3 300
和数据框2,其中这是Amercia中按活动划分的人数
Activity No of people
Activity 1 400
Activity 2 500
Activity 3 500
我想要实现的目标是:
------China Analysis------America Analysis
Activity No of people No of people
Activity 1 100 400
Activity 2 200 500
Activity 3 300 500
我想在这两个数据框上做一个合并但是我怎么做头?有什么建议吗?
答案 0 :(得分:2)
使用concat
。
如果Activity
是两个数据帧中的索引,请使用:
df = pd.concat([df1, df2], axis=1, keys=('China Analysis','America Analysis'))
如果没有,请先set_index
:
df = pd.concat([df1.set_index('Activity'),
df2.set_index('Activity')],
axis=1,
keys=('China Analysis','America Analysis'))
print (df)
China Analysis America Analysis
No of people No of people
Activity
Activity 1 100 400
Activity 2 200 500
Activity 3 300 500
答案 1 :(得分:1)
一种方法:
table1 = pd.DataFrame({'Activity':['Activity 1', 'Activity 2', 'Activity 3'], 'No of people':[100,200,300]}).rename(columns={'No of people':('No of people', 'France')})
table2 = pd.DataFrame({'Activity':['Activity 1', 'Activity 2', 'Activity 3'], 'No of people':[101,201,500]}).rename(columns={'No of people':('No of people', 'America')})
table3 = pd.DataFrame({'Activity':['Activity 1', 'Activity 2', 'Activity 3'], 'No of people':[566,22,38]}).rename(columns={'No of people':('No of people', 'Espana')})
merged = table1.merge(table2, on='Activity').merge(table3, on='Activity')
merged = merged.set_index('Activity')
merged.columns = pd.MultiIndex.from_tuples(merged.columns)
print(merged)
输出:
No of people
France America Espana
Activity
Activity 1 100 101 566
Activity 2 200 201 22
Activity 3 300 500 38