3 回答
TA贡献1906条经验 获得超10个赞
你需要:
fil_1 = test['col_a'].isin(['abc','def','ghi'])
fil_2 = test['col_b'].isin(['yes'])
fil_3 = test['col_c'].isin(['a'])
或者
test.isin({'col_a': ['abc','def','ghi'],
'col_b': ['yes'],
'col_c' :['a']}).all(axis = 1)
df_filtered = test[fil_1 & fil_2 & fil_3]
print(df_filtered)
col_a col_b col_c
0 abc yes a
2 abc yes a
4 def yes a
6 def yes a
8 ghi yes a
10 ghi yes a
或逻辑|
fil = test.isin({'col_a': ['abc','def','ghi'],'col_b': ['yes'],'col_c' :['a']})
df_filtered = df[fil]
print(df_filtered)
col_a col_b col_c
0 abc yes a
1 abc NaN NaN
2 abc yes a
3 def NaN NaN
4 def yes a
5 def NaN NaN
6 def yes a
7 def NaN NaN
8 ghi yes a
9 ghi NaN NaN
10 ghi yes a
现在,如果我们还使用DataFrame.all:
df_filtered = df[fil.all(axis = 1)]
print(df_filtered)
col_a col_b col_c
0 abc yes a
2 abc yes a
4 def yes a
6 def yes a
8 ghi yes a
10 ghi yes a
细节
print(fil)
col_a col_b col_c
0 True True True
1 True False False
2 True True True
3 True False False
4 True True True
5 True False False
6 True True True
7 True False False
8 True True True
9 True False False
10 True True True
print(test.isin({'col_a': ['abc','def','ghi']}))
col_a col_b col_c
0 True False False
1 True False False
2 True False False
3 True False False
4 True False False
5 True False False
6 True False False
7 True False False
8 True False False
9 True False False
10 True False False
这个返回False的列差异比col_a 所以你得到NaN的值是因为你正在使用&
TA贡献1862条经验 获得超7个赞
过滤数据框的可能解决方案如下:“cond1”选择 col_a 中的所有值,即“abc”或“def”或“ghi”。那么 col_b 只是“yes”,col_c 只是“a”。
cond1=(apd.col_a=="abc") | (apd.col_a=="def") | (apd.col_a=="ghi")
apd[ cond1 & (apd.col_b=="yes") & (apd.col_c=="a")]
结果:
col_a col_b col_c
0 abc yes a
2 abc yes a
4 def yes a
6 def yes a
8 ghi yes a
10 ghi yes a
TA贡献2051条经验 获得超10个赞
这是单线解决方案,
test[test.col_a.isin(['abc','def','ghi']) & test.col_b.isin(['yes']) & test.col_c.isin(['a'])]
添加回答
举报