รวมตัวกรองด้วย AND และ OR
เชื่อมหลายเงื่อนไขอย่างปลอดภัย
รวมตัวกรองด้วย AND และ OR เป็นบทเรียน Data Science Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Data Science Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
One Condition Is Rarely Enough
Real questions stack conditions: adults in Paris, or orders that are late and large. You need to combine filters cleanly. 🧩
Use the Ampersand for AND
To require both conditions, join the two masks with the ampersand. It works element by element across the rows.
df[(df["age"] > 30) & (df["city"] == "Paris")]Use the Pipe for OR
To accept either condition, join the masks with the pipe. A row passes if at least one side is True.
df[(df["city"] == "Paris") | (df["city"] == "Lyon")]Wrap Each Condition in Parentheses
Always put parentheses around each condition. Without them, Python evaluates the operators in the wrong order and raises an error.
df[df["age"] > 30 & df["score"] > 80] # breaksWhy Not and or or?
The plain words and and or work on single True/False values, not on whole Series. For row masks you must use the symbols.
# wrong: df[(df["age"] > 30) and (...)]
# right: use & insteadInvert With the Tilde
The tilde flips a mask, turning True into False. It is the clean way to say not this condition.
df[~(df["city"] == "Paris")] # everyone not in ParisMix AND and OR Carefully
You can blend operators, but parentheses decide the grouping. Make your intent explicit so the logic reads the way you mean it.
df[(df["age"] > 30) & ((c == "Paris") | (c == "Lyon"))]Three or More Conditions
Stacking more masks works the same way. For readability, build them on separate lines, then combine the named masks.
old = df["age"] > 30
top = df["score"] > 80
df[old & top]AND Narrows, OR Widens
Keep the mental model simple: AND shrinks the result toward fewer rows, while OR grows it toward more rows.
Combine Across Columns
Your conditions can touch different columns at once, letting you express rich questions in a single expression.
df[(df["age"] > 30) & (df["active"] == True)]Save Complex Masks
When logic gets dense, give the combined mask a clear name. Future you will thank you for the readable variable.
vip = (df["age"] > 30) & (df["score"] > 80)
df[vip]Quick Check
Pick the expression that filters correctly.
Recap: Combining Filters
Use the ampersand for AND, the pipe for OR, the tilde to invert, and always wrap each condition in parentheses. Complex questions, one clean line. ✅
คำถามที่พบบ่อย
บทเรียน “รวมตัวกรองด้วย AND และ OR” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “รวมตัวกรองด้วย AND และ OR” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Data Science Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รวมตัวกรองด้วย AND และ OR”
เชื่อมหลายเงื่อนไขอย่างปลอดภัย คุณปฏิบัติ Data Science Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Data Science Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Data Science Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “รวมตัวกรองด้วย AND และ OR” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Data Science Academy นี้ได้ไหม
ได้ บทเรียน Data Science Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เลือกคอลัมน์ด้วยชื่อ
- กรองแถวด้วยเงื่อนไข
- รวมตัวกรองด้วย AND และ OR
- query และ isin สำหรับตัวกรองที่อ่านง่าย