Is there a way to overwrite False to True?

Question:

I have a pandas dataframe which currently looks like this.

Original Table

Test Subject Test1 Test2 Test3
Subject1 True False False
Subject2 True False False
Subject3 True False False
Subject2 False False False
Subject2 False True True
Subject3 False False True

Is there any possible way for me to group the test subjects and overwrite previous values within their column to make it look like the following without doing it manually?

Desired Table

Test Subject Test1 Test2 Test3
Subject1 True False False
Subject2 True True True
Subject3 True False True

Attached code to make the original dataframe from scratch

import pandas as pd

df = pd.DataFrame({'Test Subject': ["Subject1", "Subject2", "Subject3", "Subject2", "Subject2", "Subject3"], 'Test1': [True, True, True, False, False, False], 'Test2': [False, False, False, True, False, False], 'Test3': [False, False, False, False, True, True]})

EDIT: The table formatting seems to be broken for some reason so I’ve posted photos below of the tables

Original Table
Original Table created

Desired Table
Desired Table from question

Asked By: Pikehex

||

Answers:

You can simply do:

df.groupby('Test Subject', as_index=False).any()

  Test Subject  Test1  Test2  Test3
0     Subject1   True  False  False
1     Subject2   True   True   True
2     Subject3   True  False   True

If any value in the group is True, it returns True

Answered By: Psidom

You can use pandas.DataFrame.groupby :

out = df.groupby('Test Subject', as_index=False).agg('sum').replace({1:True, 0:False})

>>> print(out)

enter image description here

Answered By: L'Artiste
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.