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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值