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

Concatenate multiple existing dataframes using names of dataframes included in a list

actually I am economist and not a programmer, I hope there is a good Samaritan who can help me out.

I have a list called iShares:

iShares = ['IE0005042456', 'IE00B1FZS574', 'IE00B0M62Q58', 'IE00BF4RFH31', 'IE0031442068', 'IE00BGL86Z12', 'IE00BD45KH83']

Also I have a list of dataframes with identical structure:

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

Lookup_Sector_IE0005042456
Lookup_Sector_IE00B1FZS574
Lookup_Sector_IE00B0M62Q58
Lookup_Sector_IE00BF4RFH31
Lookup_Sector_IE0031442068
Lookup_Sector_IE00BGL86Z12
Lookup_Sector_IE00BD45KH83

I would like to concatenate these dataframes to only one data frame with the name Lookup_Sector. This is possible by hardcoding the names:

Lookup_Sector = pd.concat([Lookup_Sector_IE0005042456, Lookup_Sector_IE00B1FZS574, Lookup_Sector_IE00B0M62Q58, Lookup_Sector_IE00BF4RFH31, Lookup_Sector_IE0031442068, Lookup_Sector_IE00BGL86Z12, Lookup_Sector_IE00BD45KH83])

But what I rather would like to is not to hardcode and instead be more flexible and use the list iShares by first creating another list with the names of the dataframes, called Lookup_Sector_list:

Lookup_Sector_list = ['Lookup_Sector_' + i for i in iShares]

Then in the next step I would love to concetenate by using a for loop:

for i in Lookup_Sector_list:
    Sector = pd.concat([Lookup_Sector_list[i]]) 

If I do so I get an error:

TypeError: list indices must be integers or slices, not str

How can I concatenate the dataframes without hardcoding the names and instead using the list iShares?

TY so much in advance.

>Solution :

Since all your dataframes are in the global environment, You can select them using globals()[dat] and then concatenate them:

pd.concat([globals()['Lookup_Sector_' + i ]for i in iShares])
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