Disclaimer: I tried to do this in SQL and none of the answers/my attempts worked so I have been trying to use python as it seems to be better suited
I am hoping to create a function that can delete all items in a group if any one of them matches a specific condition.
Specifically, I have a dataset 'family' and I want to delete all members of the family if that family contains twins.
A portion of the dataset looks like this:
| Subject ID | Mother_ID | Zygosity_SR |
|---|---|---|
| 1001 | 2001 | MZ |
| 1002 | 2001 | MZ |
| 1003 | 2001 | NotTwin |
| 1004 | 2002 | NotTwin |
| 1005 | 2002 | NotTwin |
In this case I want to delete all rows with individuals with the same Mother_ID as the Subjects with Zygosity_SR = MZ.
My resulting table would look like this:
| Subject ID | Mother_ID | Zygosity_SR |
|---|---|---|
| 1004 | 2002 | NotTwin |
| 1005 | 2002 | NotTwin |
This is the python code I have:
import pandas as pd
family = pd.read_excel('HCP database 97 excel vers.xlsx')
family_drop = family.groupby('Mother_ID').filter(lambda x: x['ZygositySR'].str.strip() == 'MZ' )
family_drop.reset_index(drop=True, inplace=True)
family_drop = family_drop[['Subject','Mother_ID']]
print(family_drop)
I have been getting the error:
TypeError: filter function returned a Series, but expected a scalar bool
Any tips at all on how to fix this would be greatly appreciated. Thank you so much!