Skip to content
Advertisement

pandas: Create new column by comparing DataFrame rows with columns of another DataFrame

Assume I have df1:

JavaScript

And a df2:

JavaScript

I’m looking for a way to create a new column in df2 that gets number of rows based on a condition where all columns in df1 has values greater than their counterparts in df2 for each row. For example:

JavaScript

To elaborate, at row 0 of df2, df1.alligator_apple has 4 rows which values are higher than df2.alligator_apple with the value of 6. df1.barbadine has 10 rows which values are higher than df2.barbadine with value of 3, while similarly df1.capulin_cherry has 10 rows.

Finally, apply an ‘and’ condition to all aforementioned conditions to get the number ‘4’ of df2.greater of first row. Repeat for the rest of rows in df2.

Is there a simple way to do this?

Advertisement

Answer

I believe this does what you want:

JavaScript

output:

JavaScript

Edit: if you want to generalize and apply this logic for a given column set, we can use functools.reduce together with operator.and_:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement