Home > Software design >  How to check if two pandas dataframes have same values and concatenate those rows?
How to check if two pandas dataframes have same values and concatenate those rows?

Time:12-06

I got a DF called "df" with 4 numerical columns [frame,id,x,y] I made a loop that creates two dataframes called df1 and df2. Both df1 and df2 are subseted of the original dataframe.

What I want to do (and I am not understanding how to do it) is this: I want to CHECK if df1 and df2 have same VALUES in the column called "id". If they do, I want to concatenate those rows of df2 (that have the same id values) to df1.

For example: if df1 has rows with different id values (1,6,4,8) and df2 has this id values (12,7,8,10). I want to concatenate df2 rows that have the id value=8 to df1. That is all I need

This is my code:

for i in range(0,max(df['frame']),30):
 df1=df[df['frame'].between(i, i 30)]
 df2=df[df['frame'].between(i-30, i)]

CodePudding user response:

df3 = pd.concat([df1, df2[df2.id.isin(df1.id)]], axis = 0)

  • Related