Search code examples
pythonpandasdataframeenumerate

Merging list of Dataframes depending on another list of lists


I have a list of Dataframes & a list of lists with numbers.

The idea is i want to merge the Dataframes together based on their index that matches the list of numbers that are stacked together. e.g. merge df1 & df2 because 0 & 1. Then merge df3,df4,df5,df6 because of 2,3,4,5. etc...

list_dfs=[df1, df2, df3... df50]


list_num= [[0,1], [2,3,4,5], [6], [7,8] ... [125,126,127]]

This is what i have done, i am sure it is not pythonic tho.

dfs_list=[]

for i, df in enumerate(list_dfs):
    for index,num in enumerate(list_num):
        dfs_list.append(pd.merge(dfs[i], dfs[num]))

any suggestions.


Solution

  • You don't need outer for:

    dfs_list=[]
    
    for nums in list_num:
        dfs_list.append(pd.concat([dfs[i] for i in nums], join="inner"))