Pandas: merge multiple dataframes and control column names?

后端 未结 3 2118
既然无缘
既然无缘 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:35

    I've wanted this as well at times but been unable to find a built-in pandas way of doing it. Here is my suggestion (and my plan for the next time I need it):

    1. Create an empty dictionary, merge_dict.
    2. Loop through the index you want for each of your data frames and add the desired values to the dictionary with the index as the key.
    3. Generate a new index as sorted(merge_dict).
    4. Generate a new list of data for each column by looping through merge_dict.items().
    5. Create a new data frame with index=sorted(merge_dict) and columns created in the previous step.

    Basically, this is somewhat like a hash join in SQL. Seems like the most efficient way I can think of and shouldn't take too long to code up.

    Good luck.

提交回复
热议问题