Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to overwrite False to True in the case of dataframe?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading