-1

For context, I am trying to build a script that will output information about the DataFrames I have in memory.

I have a DataFrame with all the DataFrames listed (dflist). I would like to get the Name of the Dataframe out to call a function. df1.shape for example. I am running into an issue with the system seeing df1 as a string.

dflist
|Name|
|df1 |
|df2 |

Thank You.

RMichalowski
  • 330
  • 1
  • 4
  • 11

3 Answers3

3

You can use locals(), which returns a dict of all local variables, i.e.

df1

is the same as

locals()['df1']

so you can do

[locals()[x].shape for x in list_of_names]
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
1

I assume you meant you have a list of DataFrame names corresponding to the dataframes in memory. In any case, the eval function is what you are looking for.

import pandas as pd

df1 = pd.DataFrame([0, 1])
df2 = pd.DataFrame([2, 3])
df3 = pd.DataFrame([4, 5])

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

[print(eval(x).shape) for x in list_of_names]
PejoPhylo
  • 469
  • 2
  • 11
0

Since using eval is a bad idea (Why is using 'eval' a bad practice?), I suggest that you use a dictionary:

import pandas as pd
d = {} 
d['df1'] = pd.DataFrame([0, 1])
d['df2'] = pd.DataFrame([2, 3])
d['df3'] = pd.DataFrame([4, 5])

Then, you will be able to iterate through the dataframes to compute the shape or apply any function as follows:

In [7]: d.keys() 
Out[7]: ['df1', 'df3', 'df2']

In [8]: [d[x].shape for x in d.keys()]  
Out[8]: [(2, 1), (2, 1), (2, 1)]

The extra advantage is that you can easily access any dataframe d['df1'] with no extra memory or speed loss:

In [10]: timeit d['df1']
10000000 loops, best of 3: 29.2 ns per loop
Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117