大熊猫-合并并唯一重命名具有相同列名的两个数据框的列

时间:2020-06-22 05:23:45

标签: pandas dataframe merge

我有两个数据框,如下所示:

dataframeA
bagle scom others
111   222  333
111   222  333

dataframeB
bagle scom others
444   555  666
444   555  666

我想将dataframeA和dataframeB(相同列的名称)合并为:

dataframeA&B
 A     B     C    D    E      F
111   444   222  555  333    666
111   444   222  555  333    666

我该怎么办?

1 个答案:

答案 0 :(得分:0)

我认为您不只是对获得特定结果感兴趣,而对更通用的解决方案感兴趣,您需要:

  1. 合并2个具有相似列名的DF
  2. 通过相似性重新排列列的位置
  3. 保留原始外部顺序(百吉饼,scom等)-这里使用的要点要求python> = 3.7(在其中保证OrderedDict键插入顺序)
  4. 使用某种滚动命名约定重命名相似的列(这里我使用了您的A-Z约定,但显然存在跨Z .. 的限制。)

以下代码:

import numpy as np
import pandas as pd
from collections import OrderedDict


# create the DFs
df_1 = pd.DataFrame({'bagle': [111, 111], 'scom': [222, 222], 'others': [333, 333]})
df_2 = pd.DataFrame({'bagle': [444, 444], 'scom': [555, 555], 'others': [666, 666]})

# concat them horizontally
df_3 = pd.concat([df_1, df_2], axis=1)
columns = df_3.columns

# unique list for the builtin pandas renaming to work with similar names
unique_columns = list(OrderedDict.fromkeys(columns))

# final renaming
columns_fixed = [ chr(65 + i) for i in range(len(columns)) ]

# pandas-re-ordering columns before renaming
df_3 = df_3[unique_columns]

# the actual renaming to char-based
df_3.columns = columns_fixed
df_3

##############################
    A   B   C   D   E   F
0   111 444 222 555 333 666
1   111 444 222 555 333 666

参考:

  1. pandas' concat
  2. SO "Renaming columns in a Pandas dataframe with duplicate column names"
  3. 内置chr函数
  4. SO "how-do-you-remove-duplicates-from-a-list-whilst-preserving-order"