← Back to Pandas Course | Chapter 4: Indexing & Selection | Lesson 5 of 7

query() का उपयोग

query आपको condition को एक readable string की तरह लिखकर rows filter करने देता है।

In this page:

  1. query()
Syntax
python
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()

python
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. #}
आम गलतियां
  1. string values के चारों ओर quotes भूल जाना
  2. outside variables के लिए @ इस्तेमाल न करना
  3. लंबी 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.