Skip to content Skip to sidebar Skip to footer

Comparing Values From Different Dataframes Line By Line, Python

I have two dataframes with different numbers of lines. X&Y are coordinates in 2D position DF1: X,Y,C 1,1,12 2,2,22 3,3,33 4,4,45 5,5,43 6,6,56 DF2: X,Ystart squere next two X,

Solution 1:

Check this code, checking for a 5x5 square.

DF1 = pd.DataFrame({"X":[1,2,3,4,5,6],"Y":[1,2,3,4,5,6],"C":[12,22,33,45,13,56]})
DF2 = pd.DataFrame({"X":[1,5],"Y":[1,1],"X1":[5,1],"Y1":[5,5]})

def isInSquare(row, df2):
    c1 =  (row.X > df2.iloc[0].X) and (row.Y > df2.iloc[0].Y)
    c1 = c1 and(row.X < df2.iloc[0].X1) and (row.Y < df2.iloc[0].Y1)
    c1 = c1 and(row.X < df2.iloc[1].X) and (row.Y > df2.iloc[1].Y)               
    c1 = c1 and(row.X > df2.iloc[1].X1) and (row.Y < df2.iloc[1].Y1)
    returnc1DF_NEW= DF1[DF1.apply(lambda x: isInSquare(x,DF2),axis= 1)]

output

    C   X   Y
1   22  2   2
2   33  3   3
3   45  4   4

if you want to keep the max C:

DF_NEW = DF_NEW.groupby(["X","Y"]).max().reset_index()

Post a Comment for "Comparing Values From Different Dataframes Line By Line, Python"