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

Pandas中布尔索引场景下两个print()语句的差异解析

Difference Between the Two print() Statements in Pandas Boolean Indexing

Let’s break down exactly what each statement does using your code example:

import pandas as pd
s1 = pd.Series(data=[90, 65, 43, 99], index=['path', 'pulkit', 'gaurav', 'piyush'])

1. print(s1 >= 80)

This expression compares every value in the Series s1 to 80, returning a boolean Series where each entry is True if the original value meets the condition (>=80) and False otherwise. It doesn’t filter the original data—it just gives you a "checklist" of which positions satisfy the condition.

Output:

path      True
pulkit    False
gaurav    False
piyush     True
dtype: bool

2. print(s1[s1 >= 80])

Here, we’re using that boolean Series as an index filter on the original s1. Pandas will only retain the rows where the boolean value is True, so you get back the actual numerical values from s1 that meet the condition, paired with their original indexes.

Output:

path      90
piyush    99
dtype: int64

Key Takeaway

  • The first statement tells you which entries pass the condition (via True/False flags).
  • The second statement shows you what those passing entries are (the actual data points).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 12:59:07