我有一个数据框列表的列表。
Biglist = [[dfA1, dfB11,dfB12][dfA2, dfB21,dfB22][dfA3, dfB31,dfB32][dfA4, dfB41,dfB42]]
我想从上面列表中的所有子列表创建一个 A 的数据框。
我的预期输出
df_A = concating dfA1 to dfA4
我现在的代码
df_A = [pd.concat(Biglist[i][0],axis=1) for i in range(0,len(Biglist[i]),1)]
我目前的输出
TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"
最佳答案
您需要将迭代器放在pd.concat()
的第一个参数中。此外,它不需要位于另一个列表中。
df_A = pd.concat((l[0] for l in BigList), axis=0)
我还建议您习惯使用for var in listname
而不是for i in range(len(listname))
。它使代码更简单、更清晰。如果您还需要某些内容的索引,请使用 enumerate()
。
关于Python 将特定列表的列表连接到数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59762303/