1 回答

TA貢獻1824條經驗 獲得超5個贊
也許您可以suffixes在合并中使用參數來控制列名稱。來自pandas 合并文檔:
將 DataFrame df1 和 df2 與附加到任何重疊列的指定左后綴和右后綴合并。
在上面,類似:
combine = pd.merge(file1, file2, on='filename', how='inner', suffixes=('_file1', '_file2'))
其他方面也類似merge。這樣你就可以在合并時知道計數來自哪里。
例子:
# Creating Dataframes
df1 = pd.DataFrame({'col1': ['foo', 'bar', 'baz'], 'count': [1, 2, 3]})
df2 = pd.DataFrame({'col1': ['foo', 'bar', 'baz'], 'count': [5, 6, 7]})
df1:
col1 count
0 foo 1
1 bar 2
2 baz 3
df2:
col1 count
0 foo 5
1 bar 6
2 baz 7
合并
pd.merge(df1, df2, on='col1', suffixes=('_df1', '_df2'))
結果:
col1 count_df1 count_df2
0 foo 1 5
1 bar 2 6
2 baz 3 7
更新
鑒于您有四個數據框,也許您可以嘗試:
# Combine two of them
combine1 = pd.merge(file1, file2, on='filename', how='inner', suffixes=('_file1', '_file2'))
# Combine other two
combine2 = pd.merge(file3, file4, on='filename', how='inner', suffixes=('_file3', '_file4'))
# Now combine the combined dataframes
combine = pd.merge(combine1, combine2, on='filename', how='inner')
添加回答
舉報