我有一个像这样的数据框
df = pd.DataFrame({'patient': ['patient1', 'patient1', 'patient1','patient2', 'patient2', 'patient3','patient3','patient4'],
'gene':['TYR','TYR','TYR','TYR','TYR','TYR','TYR','TYR'],
'variant': ['buu', 'luu', 'stm','lol', 'bla', 'buu', 'lol','buu'],
'genotype': ['hom', 'het', 'hom','het', 'hom', 'het', 'het','het']})
df
patient gene variant genotype
0 patient1 TYR buu hom
1 patient1 TYR luu het
2 patient1 TYR stm hom
3 patient2 TYR lol het
4 patient2 TYR bla hom
5 patient3 TYR buu het
6 patient3 TYR lol het
7 patient4 TYR buu het
我想识别患有 buu 和其他变异但不患有 luu 的患者。所以,预期的输出应该是这样的
patient gene variant genotype
patient3 TYR buu het
patient3 TYR lol het
我该怎么做?
最佳答案
您还可以尝试以下解决方案:
import pandas as pd
# First we filter out those groups that have only 1 observations
g = df.groupby('patient').filter(lambda x: len(x) > 1)
# Then we apply both of our desired conditions
m = (g.groupby('patient')['variant'].transform(lambda x: x.eq('buu').any() & (~ x.eq('luu').any())))
g.loc[m]
patient gene variant genotype
5 patient3 TYR buu het
6 patient3 TYR lol het
关于python - 如何过滤一列中的值与另一个公共(public)列中的公共(public)值并在过滤中应用两个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75914405/