如何在Python中从列表中串联两个数据框(具有相同名称和相同列)?

问题描述 投票:0回答:1
df_list1=[A,B,C]
df_list2=[A,B,C]

我想使用pandas concat将A(df_list1)连接到A(df_list2),将B连接到B,就像没有循环一样。这里的A,B,C是数据帧。我尝试过迭代一个列表,并且一次只串联每个列表中的一个数据框。

i=0
for df in df_list1:
   l=[df,df_list2[i]]
   df=pd.concat(l)
   i=i+1

有人可以帮助我无循环执行此操作,因为我有一定​​的时间执行要求(基本上我需要减少时间)?

python pandas list dataframe concat
1个回答
0
投票

我认为您不能使用循环来过去,但是您可能会对zip函数感兴趣,该函数在您遍历各个列表时会匹配各个列表的元素。

df_list1=[A,B,C]
df_list2=[A,B,C]
concat_list = []

for x, y in zip(df_list1, df_list2):
    concat_list.append(pd.concat([x, y]))

编辑:

实际上,您可以使用map

concat_list = list(map(lambda x: pd.concat([x[0], x[1]), zip(df_list1, df_list2)))
© www.soinside.com 2019 - 2024. All rights reserved.