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
相关产品推荐
相关产品推荐

