The query() Method
The query() method provides a SQL-like syntax for filtering DataFrames. It's often more readable than boolean indexing.
Basic Query Syntax
Write conditions as strings:
Loading Pandas Playground...
Comparison with Boolean Indexing
Compare readability:
Loading Pandas Playground...
Using Variables in Queries
Reference Python variables with @:
Loading Pandas Playground...
Complex Queries
Combine conditions with and, or, not:
Loading Pandas Playground...
Query with in and not in
Check membership in lists:
Loading Pandas Playground...
String Methods in Query
Use string methods with .str:
Loading Pandas Playground...
Exercise: Simple Query
Loading Exercise...
Exercise: Query with Variable
Loading Exercise...
Key Points
query()uses string expressions for filtering- More readable than boolean indexing
- Use
@variableto reference Python variables - Use
and,or,notinstead of&,|,~ - Use
inandnot infor membership checks - For string methods, stick with
.straccessor
التالي · الدرس 12 من 42
Sorting Data
قراءة 1 دقيقة
أنهيت 11 من 42 درسًا

اسأل Byte عن هذا الدرس
محادثة خاصة حول هذا الدرس. قرأ Byte الدرس كاملًا، ويجيب خلال ثوانٍ، ويُظهر القسم الذي تأتي منه كل إجابة. يمكنك السؤال بأي لغة.
الأسئلة والأجوبة
أسئلة عامة حول هذا الدرس. يجيب AI Tutor فورًا بلغتك، ويراجع فريق FreeAcademy الإجابات، ويمكن لكل المتعلمين قراءتها.

