简体   繁体   English

如何在python中遍历两个数据帧

[英]How to iterate through two data frames in python

I have two Data frames df1 ( having columns C1,C2,etc) and df2 (having columns S1,S2,etc) 我有两个数据帧df1 (具有列C1,C2等)和df2 (具有列S1,S2等)
I want to iterate through each column of both the Data Frames. 我想遍历两个数据框的每一列。
Currently I am doing the following thing: 目前,我正在做以下事情:

df3=pd.Dataframe([])
for index1,row1 in df1.iterrows():
    for index2,row2 in df2.iterrows():
        if row1['C1']==row2['S1']:
            #perform Some Operations on each row like:
            df3 = df3.append(pd.DataFrame({'A': row2['S1'], 'B': row2['S2'],'C':functionCall(row1['c3'], row2['S3'])}, index=[0]), ignore_index=True)  

This works ok but it takes too much time. 可以,但是需要太多时间。
I wanted to know, Is there a more efficient way of iterating through two Data Frames? 我想知道,是否有一种更有效的方法来遍历两个数据帧?

I think need merge first, then apply function and last filter columns by subset - [[]] : 我认为需要先merge ,然后再按子集- [[]] apply函数和最后过滤器列:

df3 = pd.merge(df1, df2, left_on='C1', right_on='S1')
df3['C'] = df3.apply(lambda x: functionCall(x['C3'], x['S3']), axis=1)
df3 = df3[['S1', 'S2', 'C']].rename(columns={'S1': 'A','S2': 'B'})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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