Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas concetenation using list

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

Just check eval

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading