简体   繁体   中英

How to Concatenate two dataframes(with same names and same columns) from List without loop in Python?

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

I want to use pandas concat to concatenate A(df_list1) to A(df_list2), B to B like wise without loop. Here A, B, C are dataframes. I have tried iterating one of the list and just concatenating one dataframe from each list at a time.

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

Could someone help me doing this without loop as I have certain time execution requirement(basically I need to reduce time)?

I don't think you can get past using a loop, but you may be interested in the zip function, which matches the elements of respective lists as you iterate through them.

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]))

EDIT:

Actually, you could use map :

concat_list = list(map(lambda x: pd.concat([x[0], x[1]), zip(df_list1, df_list2)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM