Skip to content
Advertisement

Find max of two columns and populate with value in third column based on a condition

I have a pandas dataframe with 4 columns – A, B, C,D and E. I want create groups by column A and then find the max of column B and C and then populate E with the corresponding value present in D as shown in example below. The condition here is that Column E is to be populated with 0, if a group in column B has all 1’s.

My expected output

JavaScript

My code:

JavaScript

This code works fine when there is number greater than 2, but when when column B has all 1 for any group, it is not populating 0’s(which is what I need to do). When I add the if condition , the group with number greater than 2 also shows 0.

The line of code where it is getting messed up

JavaScript

Advertisement

Answer

Try:

JavaScript

Prints:

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