Walt Reed
Walt Reed

Reputation: 1466

Pandas - List of Dataframe Names?

I've done a lot of searching and can't find anything related. Is there a built-in function to automatically generate a list of Pandas dataframes that I've created?

For example, I've created three dataframes: df1 df2 df3

Now I want a list like: df_list = [df1, df2, df3] so I can iterate through it.

Upvotes: 3

Views: 8935

Answers (2)

soundboyselecta
soundboyselecta

Reputation: 21

%who_ls DataFrame

This is all dataframes loaded in memory as a list

all_df_in_mem = %who_ls DataFrame

Upvotes: 1

piRSquared
piRSquared

Reputation: 294488

My hacky way to get them (-:

Python provides a built-in globals that returns a dictionary where keys are named variables and values are the objects themselves. We can use a list comprehension and iterate over all values of the dictionary and only get those that are of type pd.DataFrame

d = globals()
df_list = [v for k, v in d.items() if isinstance(v, pd.DataFrame)]

Upvotes: 6

Related Questions