Python Pandas - Concat dataframes with different columns ignoring column names

前端 未结 3 432
粉色の甜心
粉色の甜心 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:43

    I am not sure if this will be simpler than what you had in mind, but if the main goal is for something general then this should be fine with one assumption: The columns in the two files match for example if date is the first column, the translated version will also be the first column.

    # number of columns
    n_columns = len(df_ger.columns)
    
    # save final columns names
    columns = df_uk.columns
    
    # rename both columns to numbers
    df_ger.columns = range(n_columns)
    df_uk.columns = range(n_columns)
    
    # concat columns
    df_out = pd.concat([df_ger, df_uk], axis=0, ignore_index=True)
    
    # rename columns in new dataframe
    df_out.columns = columns
    

提交回复
热议问题