Pandas: merge multiple dataframes and control column names?

后端 未结 3 2258
既然无缘
既然无缘 2021-02-06 12:03

I would like to merge nine Pandas dataframes together into a single dataframe, doing a join on two columns, controlling the column names. Is this possible?

I have nine d

3条回答
  •  我寻月下人不归
    2021-02-06 12:23

    Would doing a big pd.concat() and then renaming all the columns work for you? Something like:

    desired_columns = ['items', 'spend']
    big_df = pd.concat([df1, df2[desired_columns], ..., dfN[desired_columns]], axis=1)
    
    
    new_columns = ['org', 'name']
    for i in range(num_dataframes):
        new_columns.extend(['spend_df%i' % i, 'items_df%i' % i])
    
    bid_df.columns = new_columns
    

    This should give you columns like:

    org, name, spend_df0, items_df0, spend_df1, items_df1, ..., spend_df8, items_df8

提交回复
热议问题