pandas join DataFrame force suffix?

后端 未结 4 1574
暖寄归人
暖寄归人 2021-02-05 02:32

How can I force a suffix on a merge or join. I understand it\'s possible to provide one if there is a collision but in my case I\'m merging df1 with df2 which doesn\'t cause an

4条回答
  •  误落风尘
    2021-02-05 03:11

    Pandas merge will give the new columns a suffix when there is already a column with the same name, When i need to force the new columns with a suffix, i create an empty column with the name of the column that i want to join.

    df["colName"] = "" #create empty column 
    df.merge(right = "df1", suffixes = ("_a","_b"))
    

    You can later drop the empty column.

    You could do the same for more than one columns, or for every column in df.columns.values

提交回复
热议问题