Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pandas-合并并唯一重命名具有相同列名的两个数据帧的列_Pandas_Dataframe_Merge - Fatal编程技术网

pandas-合并并唯一重命名具有相同列名的两个数据帧的列

pandas-合并并唯一重命名具有相同列名的两个数据帧的列,pandas,dataframe,merge,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(相同列的名称)合并到: 我该怎么办?我想您不想只得到这个特定的结果,而是想得到一个更通用的解决方案,您需要: 合并具有相似列名的2个DFs 通过相似性重新排序列的位置 保留原始外部顺

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

dataframeA
bagle scom others
111   222  333
111   222  333

dataframeB
bagle scom others
444   555  666
444   555  666
我想将dataframeA和dataframeB(相同列的名称)合并到:


我该怎么办?

我想您不想只得到这个特定的结果,而是想得到一个更通用的解决方案,您需要:

  • 合并具有相似列名的2个DFs
  • 通过相似性重新排序列的位置
  • 保留原始外部顺序(bagle、scom、其他)-此处使用的要点要求python>=3.7(其中OrderedDict键插入顺序得到保证)
  • 使用某种滚动命名约定重命名类似的列(这里我使用了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
    
    参考资料:

  • 内置函数

  • 我假设您不想只得到这个特定的结果,而是想得到一个更通用的解决方案,其中要求您:

  • 合并具有相似列名的2个DFs
  • 通过相似性重新排序列的位置
  • 保留原始外部顺序(bagle、scom、其他)-此处使用的要点要求python>=3.7(其中OrderedDict键插入顺序得到保证)
  • 使用某种滚动命名约定重命名类似的列(这里我使用了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
    
    参考资料:

  • 内置函数