query() का उपयोग
query आपको condition को एक readable string की तरह लिखकर rows filter करने देता है।
In this page:
Syntax
df.query("column > value and other_column == 'text'")
query()
df.query("age > 30 and city == Oslo") plain English की तरह पढ़ा जाता है और and, or, और not allow करता है। @ prefix से Python variables reference करें। यह boolean masks से ज़्यादा compact हो सकता है, खासकर लंबी conditions के लिए।
Note:
spaces वाले column names के लिए, इन्हें backticks में wrap करें।
उदाहरण: query()
import pandas as pd
df = pd.DataFrame({"name": ["Ann", "Bob", "Cy"], "age": [28, 35, 41], "city": ["Oslo", "Rome", "Oslo"]})
limit = 30
print(df.query("age > @limit and city == 'Oslo'"))
print(df.query("name in ['Ann', 'Bob']"))
# Output:
# name age city
# 2 Cy 41 Oslo
# name age city
# 0 Ann 28 Oslo
# 1 Bob 35 Rome
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. #}
आम गलतियां
- string values के चारों ओर quotes भूल जाना
- outside variables के लिए @ इस्तेमाल न करना
- लंबी complex logic जो masks की तरह clearer है
चैप्टर सारांश
- query एक condition string लेता है
- and, or, not इस्तेमाल करें
- @var variables reference करता है
- Backticks odd column names quote करते हैं
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: