Column order in pandas.concat

后端 未结 6 1655
既然无缘
既然无缘 2021-02-04 00:26

I do as below:

data1 = pd.DataFrame({ \'b\' : [1, 1, 1], \'a\' : [2, 2, 2]})
data2 = pd.DataFrame({ \'b\' : [1, 1, 1], \'a\' : [2, 2, 2]})
frames = [data1, data2         


        
6条回答
  •  后悔当初
    2021-02-04 00:57

    def concat_ordered_columns(frames):
        columns_ordered = []
        for frame in frames:
            columns_ordered.extend(x for x in frame.columns if x not in columns_ordered)
        final_df = pd.concat(frames)    
        return final_df[columns_ordered]       
    
    # Usage
    dfs = [df_a,df_b,df_c]
    full_df = concat_ordered_columns(dfs)
    

    This should work.

提交回复
热议问题