Skip to content
Advertisement

Change all values in column if a condition is met within a group in Pandas dataframe

I have a dataframe that contains many rows, and a condition that is checked for each row and saved as a boolean in a column named condition. If this condition is False for any row within a group, I want to create a new column that is set to False for the whole group, and to True if the condition for every row within the group is set to True.

The final dataframe should look like this:

JavaScript

I have tried many different things but can’t find a solution, so any help is appreciated.

Advertisement

Answer

use groupby()+transform():

JavaScript

output of df:

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