我正在尝试两个数据帧之间的合并。每个数据框有两个索引级别(日期,cusip)。例如,在列中,两列(货币,调整日期)之间的某些列匹配。熊猫合并 - 如何避免重复列
什么是最好的方式来合并这些索引,但不采取两个货币和日期的副本。
每个数据框都是90列,所以我试图避免手写所有东西。
df: currency adj_date data_col1 ...
date cusip
2012-01-01 XSDP USD 2012-01-03 0.45
...
df2: currency adj_date data_col2 ...
date cusip
2012-01-01 XSDP USD 2012-01-03 0.45
...
如果我做的:
dfNew = merge(df, df2, left_index=True, right_index=True, how='outer')
我得到
dfNew: currency_x adj_date_x data_col2 ... currency_y adj_date_y
date cusip
2012-01-01 XSDP USD 2012-01-03 0.45 USD 2012-01-03
谢谢! ...
实际的解决方案可能是删除虚假的列。不过,我很乐意看到更好的答案。 – Marcin
为什么不选择你想合并的列就像这样:'dfNew = merge(df,df2 [['data_col_2']],left_index = True,right_index = True,how ='outer')'这样可以避免重复列和冲突 – EdChum
我同意较小的数据框,但每个数据框都是90列,并且可能有10个重叠列。 – user1911092