Issue
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
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
Answered By - Psidom
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.