我有多个不同的熊猫数据框,我想阅读所有它们。由于变量名称不同,我该如何使用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']
答案 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']]
希望有帮助