Boolean Indexing का उपयोग
सिर्फ वे rows रखने के लिए जो इसे satisfy करती हैं brackets के अंदर एक condition रखें।
In this page:
Syntax
df[df["column"] > value]
df[(df["column1"] > a) & (df["column2"] == b)]
Boolean Indexing
df["age"] > 30 जैसी एक condition एक boolean Series produce करती है। df[condition] उन rows को रखता है जहाँ यह True हो। &, |, और ~ से conditions combine करें, हर एक parentheses में wrapped, और common tests के लिए isin, between, और str.contains इस्तेमाल करें।
Note:
df[~mask] वे rows रखता है जो match नहीं करतीं।
उदाहरण: Boolean indexing
import pandas as pd
df = pd.DataFrame({"name": ["Ann", "Bob", "Cy"], "age": [28, 35, 41], "city": ["Oslo", "Rome", "Oslo"]})
print(df[df["age"] > 30])
print(df[(df["city"] == "Oslo") & (df["age"] > 30)])
print(df[df["name"].isin(["Ann", "Cy"])])
# Output:
# name age city
# 1 Bob 35 Rome
# 2 Cy 41 Oslo
# name age city
# 2 Cy 41 Oslo
# name age city
# 0 Ann 28 Oslo
# 2 Cy 41 Oslo
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- conditions के बीच and / or इस्तेमाल करना
- parentheses भूल जाना
- Filter करना फिर एक copy पर assign करना
चैप्टर सारांश
- एक condition एक boolean Series देती है
- Brackets rows filter करते हैं
- & | ~ से combine करें
- isin और between tests simplify करते हैं
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: