I would like to be able to iterate over all data frames (not the names!) that are currently in the workspace.
To get a list of all data frames I found the following solution here:
import pandas as pd
# create dummy dataframes
df1 = pd.DataFrame({'Col1' : list(range(100))})
df2 = pd.DataFrame({'Col1' : list(range(100))})
# check whether all variables in scope are pandas dataframe.
# Dir() will return a list of string representations of the variables.
# Simply evaluate and test whether they are pandas dataframes
alldfs = [var for var in dir() if isinstance(eval(var), pd.core.frame.DataFrame)]
print(alldfs) # df1, df2
This works as described, but the problem is that I would like to be able to use this list to iterate over the actual data frames, not the names of the data frames.
The following code returns the length of the names of the data frames but should return the length of the data frames (number of lines):
for df in alldfs:
print(len(df))
It should return:
100
100
It returns:
3
3
How can I fix this?