【发布时间】:2020-08-09 13:47:51
【问题描述】:
我希望能够遍历当前工作区中的所有数据框(不是名称!)。
为了获得所有数据框的列表,我找到了以下解决方案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
这如描述的那样工作,但问题是我希望能够使用这个列表来迭代实际的数据帧,不是数据帧的名称。
以下代码返回数据帧名称的长度,但应该返回数据帧的长度(行数):
for df in alldfs:
print(len(df))
它应该返回:
100
100
返回:
3
3
我该如何解决这个问题?
【问题讨论】: