将多个数据帧从列表传递到新的数据帧

2024-05-19 10:29:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我在列表中遇到了一个问题,列表中的每个值都有两列,就像整个数据帧一样。如何将每个值传递到新的数据帧中? 假设我有三个数据帧,它们被加载到列表名A中,如下所示

df10 = pd.DataFrame({'one' : [1., 2., 3., 4.], 'two' : [4., 3., 2., 1.]}) 
df20 = pd.DataFrame({'one' : [1., 2., 3., 4.], 'two' : [4., 3., 2., 1.]}) 
df30 = pd.DataFrame({'one' : [1., 2., 3., 4.], 'two' : [4., 3., 2., 1.]}) 
A = [df10,df20,df30] 

这方面的输出将是:

[ one two 0 1.0 4.0 1 2.0 3.0 2 3.0 2.0 3 4.0 1.0, one two 0 1.0 4.0 1 2.0 3.0 2 3.0 2.0 3 4.0 1.0, one two 0 1.0 4.0 1 2.0 3.0 2 3.0 2.0 3 4.0 1.0]

现在我想从列表中取回这些数据帧,我该怎么做


Tags: 数据dataframe列表onepdtwodf10df20
1条回答
网友
1楼 · 发布于 2024-05-19 10:29:36

如@Lambda在注释中所述,使用索引访问列表的各个项目。有关更多信息,请参见python tutorial

import pandas as pd

# define 3 different dataframes
df10 = pd.DataFrame({'one' : [1., 2., 3., 4.], 'two' : [4., 3., 2., 1.]}) 
df20 = pd.DataFrame({'one' : [3., 4., 5., 6.], 'two' : [6., 5., 4., 3.]}) 
df30 = pd.DataFrame({'one' : [7., 8., 9., 0.], 'two' : [0., 9., 8., 7.]}) 

# combine them in the list
A = [df10,df20,df30] 

# access each item of the list (dataframe) directly by using index (A[i])
print(A[0],"\n")
print(A[1],"\n")
print(A[2],"\n")

# access all items (dataframes) in the loop
for x in A:
    print(x,"\n")

相关问题 更多 >

    热门问题