当前位置:   article > 正文

python 多个条件筛选_python - Pandas多个条件的筛选_others_酷徒编程知识库

python 多个条件筛选_python - Pandas多个条件的筛选_others_酷徒编程知识库

你需要: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 adf_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 Trueprint(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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/600974
推荐阅读
相关标签
  

闽ICP备14008679号