Python Pandas - Concat dataframes with different columns ignoring column names

前端 未结 3 414
粉色の甜心
粉色の甜心 2020-12-31 00:55

I have two pandas.DataFrames which I would like to combine into one. The dataframes have the same number of columns, in the same order, but have column headings in different

3条回答
  •  礼貌的吻别
    2020-12-31 01:41

    If the columns are always in the same order, you can mechanically rename the columns and the do an append like:

    Code:

    new_cols = {x: y for x, y in zip(df_uk.columns, df_ger.columns)}
    df_out = df_ger.append(df_uk.rename(columns=new_cols))
    

    Test Code:

    df_ger = pd.read_fwf(StringIO(
        u"""
            index  Datum   Zahl1   Zahl2
            0      1-1-17  1       2
            1      2-1-17  3       4"""),
        header=1).set_index('index')
    
    df_uk = pd.read_fwf(StringIO(
        u"""
            index  Date    No1     No2
            0      1-1-17  5       6
            1      2-1-17  7       8"""),
        header=1).set_index('index')
    
    print(df_uk)
    print(df_ger)
    
    new_cols = {x: y for x, y in zip(df_uk.columns, df_ger.columns)}
    df_out = df_ger.append(df_uk.rename(columns=new_cols))
    
    print(df_out)
    

    Results:

             Date  No1  No2
    index                  
    0      1-1-17    5    6
    1      2-1-17    7    8
    
            Datum  Zahl1  Zahl2
    index                      
    0      1-1-17      1      2
    1      2-1-17      3      4
    
            Datum  Zahl1  Zahl2
    index                      
    0      1-1-17      1      2
    1      2-1-17      3      4
    0      1-1-17      5      6
    1      2-1-17      7      8
    

提交回复
热议问题