How to append selected columns to pandas dataframe from df with different columns

前端 未结 2 1561
春和景丽
春和景丽 2021-01-17 22:15

I want to be able to append df1 df2, df3 into one df_All , but since each of the dataframe has different column. How could I do this in for loop ( I have others stuff that i

2条回答
  •  不知归路
    2021-01-17 23:00

    You can also use set comprehension to join all common columns from an arbitrary list of DataFrames:

    df_list = [df1, df2, df3]
    common_cols = list(set.intersection(*(set(c) for c in df_list)))
    df_new = pd.concat([df[common_cols] for df in df_list], ignore_index=True)
    >>> df_new 
        A  B
    0   1  4
    1   2  5
    2   3  6
    3   8  5
    4   9  6
    5  10  7
    6   1  4
    7   2  5
    8   3  7
    

提交回复
热议问题