0

Input

mydfs= [df1,df2,df3,df4,df5,df6,df7,df8,df9]

My Code

import pandas as pd

df_1 = pd.concat([mydfs[0],mydfs[1],mydfs[2]])

df_m = df_1.merge(mydfs[2])

df_2 = pd.concat([mydfs[3],mydfs[4],mydfs[5]])

df_m1 = df_2.merge(mydfs[5])

df_3 = pd.concat([mydfs[6],mydfs[7],mydfs[8]])

df_m2 = df_3.merge(mydfs[8])

But I want my code dynamic way instead of doing manually, using for loop is it possible? may be in future the list of data frames will increase

mozway
  • 194,879
  • 13
  • 39
  • 75
harish m
  • 1
  • 1

2 Answers2

0

You can use a dictionary comprehension:

N = 3
out_dfs = {f'df_{i//N+1}': pd.concat(mydfs[i:i+N])
           for i in range(0, len(mydfs), N)}

output:

{'df_1': <concatenation result of ['df1', 'df2', 'df3']>,
 'df_2': <concatenation result of ['df4', 'df5', 'df6']>,
 'df_3': <concatenation result of ['df7', 'df8', 'df9']>,
}
mozway
  • 194,879
  • 13
  • 39
  • 75
0

You can use a loop with "globals" to iterate through mydfs and define two "kth" variables each round

i = 0
k = 1
while i < len(mydfs):
    globals()["df_{}".format(k)]  = pd.concat([mydfs[i],mydfs[i+1],mydfs[i+2]])
    globals()["df_m{}".format(k)] = globals()["df_{}".format(k)].merge(mydfs[i+2])
    i = i+3
    k = k+1
Edoardo Berardo
  • 142
  • 2
  • 9
  • 1
    dynamically generating variables is most often a [bad](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) [idea](https://stackoverflow.com/questions/35458691/is-it-a-good-idea-to-dynamically-create-variables), [other ref](https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval) – mozway Mar 02 '22 at 10:31
  • I quite agree. Hovewer, it still depends on what you are doing. – Edoardo Berardo Mar 02 '22 at 11:17