Home > Blockchain >  Pandas concetenation using list
Pandas concetenation using list

Time:06-17

I have the following list

list= ['df1', 'df2', 'df3']

furthermore i have the dataframes df1 df2 df3 with the same dataframe structure

Now I would like to concetenate the three dataframes using the list instead of saying

df= pd.concat([df1, df2, df3])

If I try

df= pd.concat(list)

I get the following error:

TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid

If I try

df = pd.concat([globals()[i]for i in list])

I get

KeyError: 'df1'

How can I use the list for concatenation?

CodePudding user response:

Just check eval

l = ['df1', 'df2', 'df3']
out = pd.concat([eval(x) for x in l])

CodePudding user response:

You shouldn't use globals() for that... Store the DataFrames in a dictionary instead:

dfs_dict = {
    'df1': df1,
    'df2': df2,
    'df3': df3,
     # others...
}

Then you can define a function that given a list of the df names/ keys, returns a list of the corresponding DataFrames

def get_dfs(df_names):
    return [dfs_dict[name] for name in df_names]

Finally, you can use it with pd.concat

df_names = ['df1', 'df2', 'df3']
df = pd.concat(get_dfs(df_names))
  • Related