Merging two pandas dataframes results in “duplicate” columns

后端 未结 3 1085
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 10:46

I\'m trying to merge two dataframes which contain the same key column. Some of the other columns also have identical headers, although not an equal number of rows, and after

3条回答
  •  误落风尘
    2020-12-30 11:18

    Not exactly the answer, but pd.merge provides an argument to help you decide which suffixes should be added to your overlapping columns:

    merge_df = pd.merge(holding_df, invest_df, on='key', how='left', suffixes=('_holding', '_invest')).fillna(0)
    

    More meaningful names could be helpful if you decide to keep both (or to check why the columns are kept).

    See documentation for more reference.

提交回复
热议问题