我如何使用for循环读取多个熊猫数据框

时间:2019-07-19 08:18:59

标签: python-3.x pandas for-loop

我有多个不同的熊猫数据框,我想阅读所有它们。由于变量名称不同,我该如何使用for循环。

df1=pd.DataFrame({'index':1,'text':['this is text in df1','this is text in df1']})
df2=pd.DataFrame({'index':1,'text':['this is text in df2','this is text in df2']})
df3=pd.DataFrame({'index':1,'text':['this is text in df3','this is text in df3']})

我尝试过

df_names=[df1,df2,df3]
for item in df_names['text']:
    print(item)

预期输出是一个包含每个df文本的不同列表 例子

lst1=['this is text in df1','this is text in df1']

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码:

ls1=[]
ls2=[]
ls3 =[]
names = [df1,df2,df3]
ls_name = [ls1,ls2,ls3]
for i,j in zip(names,ls_name):
    j.append(i['text'].to_list())
for x in ls_name:
    print(x)


[['this is text in df1', 'this is text in df1']]
[['this is text in df2', 'this is text in df2']]
[['this is text in df3', 'this is text in df3']]

希望有帮助