[英]Pandas merge 3 dataframes with same columns
我有 3 個數據框,其中有一個要合並的字符串列和 2 個要加起來的類似列
df1:
index user x y
1 john 5 6
2 pete 10 10
df2:
index user x y
1 john 1 1
2 pete 2 2
3 nash 5 5
DF3:
index user x y
1 nash 6 7
2 john 2 4
3 kyle 3 3
我想要:df4:
index user x y
1 john 8 11
2 pete 12 12
3 nash 11 12
4 kyle 3 3
試試這個,首先pandas.concat
然后groupby
import pandas as pd
pd.concat([df1, df2, df3]).groupby(["user"], as_index=False)[['x', 'y']].sum()
user x y
0 john 8 11
1 kyle 3 3
2 nash 11 12
3 pete 12 12
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.