且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

pandas 合并-如何避免重复的列

更新时间:2022-12-10 15:50:11

您可以算出仅在一个DataFrame中的列,并使用它来选择合并中的一部分列.

You can work out the columns that are only in one DataFrame and use this to select a subset of columns in the merge.

cols_to_use = df2.columns.difference(df.columns)

然后执行合并(请注意,这是一个索引对象,但是它具有方便的tolist()方法).

Then perform the merge (note this is an index object but it has a handy tolist() method).

dfNew = merge(df, df2[cols_to_use], left_index=True, right_index=True, how='outer')

这将避免合并中的任何列冲突.

This will avoid any columns ***ing in the merge.