我有一个df
df = pd.DataFrame({'s1':[1,1],'s2':['-',1],'s3':[1,'-'], 's4':[0,1], 's5':[0,1], 's6':['-',1], 's7':[0,'-'], 's8':[0,0]})
s1 s2 s3 s4 s5 s6 s7 s8
0 1 - 1 0 0 - 0 0
1 1 1 - 1 1 1 - 0
我想根据多列条件选择行,这样
在s1,s2,s3或s4中至少观察到一次“ 1”,而在s5,s6,s7或s8中未观察到“ 1”
或
在s5,s6或s7中至少观察到“ 1”,而在s1,s2,s3,s4,s7或s8中未观察到“ 1”
结果应该只是第一行
答案 0 :(得分:2)
定义两组条件并使用布尔索引,
cond1 = df[['s1', 's2', 's3','s4']].eq(1).any(1)
cond2 = df[['s5', 's6', 's7','s8']].ne(1).all(1)
df.loc[(cond1 & cond2)]
s1 s2 s3 s4 s5 s6 s7 s8
0 1 - 1 0 0 - 0 0
答案 1 :(得分:0)
在这里:
condition_1 = ((df.s1 == 1) | (df.s2 == 1) | (df.s3 == 1) | (df.s4 == 1)) & (df.s5 != 1) & (df.s6 != 1) & (df.s7 != 1) & (df.s8 != 1)
condition_2 = ((df.s5 == 1) | (df.s6 == 1) | (df.s7 == 1)) & (df.s1 != 1) & (df.s2 != 1) & (df.s3 != 1) & (df.s4 != 1) & (df.s8 != 1)
df[condition_1 | condition_2].head(1)
输出:
s1 s2 s3 s4 s5 s6 s7 s8
0 1 - 1 0 0 - 0 0