Home > Enterprise >  Find and return matching rows across multiple DataFrames
Find and return matching rows across multiple DataFrames

Time:06-24

Good Day,

My intention is to find the rows which contain matching values across multiple DataFrames, (in consideration of columns [0,1,2,3,4,5] as the remaining columns will contain different values) and return those rows relevant to df1 (or any, as I can then just look up the returned values for any df).

I do not know the values within the potentially matched rows, as this is what I'm seeking to discover.

I've done my best in attempting all sorts of merge, groupby, or combining everything into one DataFrame but, I havn't had much success. My best attempt is iterating through each row, return the indices for one of the frames, and carry on from there.

I do appreciate the help.

oneIndex = []

for i, row1 in df1_sorti.iloc[:,[0,1,2,3,4,5]].iterrows():
    for j, row2 in df2.iloc[:,[0,1,2,3,4,5]].iterrows():
        for k, row3 in df3.iloc[:,[0,1,2,3,4,5]].iterrows():
            for l, row4 in df4.iloc[:,[0,1,2,3,4,5]].iterrows():
                for m, row5 in df5.iloc[:,[0,1,2,3,4,5]].iterrows():
                    for n, row6 in df6.iloc[:,[0,1,2,3,4,5]].iterrows():

                        if row1.equals(row2) and row1.equals(row3) and row1.equals(row4) and row1.equals(row5) and row1.equals(row6):
                            oneIndex.append(i)
                            break

df1.iloc[oneIndex]

CodePudding user response:

df1,df2,df3 = df1.values.tolist(), df2.values.tolist(), df3.values.tolist()

oneIndex = []
for index, rows in df.iterrows():
    row = list(rows.values)
    if row in df1 and row in df2 and row in df3:
         oneIndex.append(index)
df.loc[oneIndex,:]

By converting all other 3 dataframes as list of lists, we can do this in just one loop

  • Related