You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pandas中按Pos条件筛选A、B列中≥20的值?

解决方法

你可以通过布尔掩码结合where方法实现需求,具体步骤如下:

  1. 先创建一个掩码,筛选出Pos取值为1或3的行:
mask = t['Pos'].isin([1, 3])
  1. 对A、B列做≥20的判断,再用where指定:仅在掩码为True的行保留判断结果,其他行直接设为True:
result = t[["A", "B"]].ge(20).where(mask, True)

执行后得到的result就是预期结果:

A      B
0  False   True
1   True   True
2   True  False

另外也可以用另一种方式:先初始化全为True的结果集,再给符合条件的行赋值判断结果:

result = pd.DataFrame(True, index=t.index, columns=["A", "B"])
result.loc[mask] = t.loc[mask, ["A", "B"]].ge(20).values

两种方法都能达到预期效果,选你顺手的就行。

内容的提问来源于stack exchange,提问作者Agathe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:36:29