Sangeeth Mukundan
Sangeeth Mukundan

Reputation: 742

How to get existing pandas dataframe from a string variable?

I have a lot of dataframes which are sequentially named in the format "set1", "set2", "set3" ....

I need to run a for loop across all the dataframes to carry out various operations in this dataframes.

How will I convert the string "set1" to the dataframe set1 so that I can carry out operations like set1.index, set1.columns e.t.c

Upvotes: 0

Views: 45

Answers (1)

mikaël
mikaël

Reputation: 463

Depending on whether the variables are located in the global environment or are local to the scope you are working in, you can use

## access global variable by string name
globals()['set1']

## access local variable by string name
locals()['set1']

Upvotes: 1

Related Questions