กรองแถวด้วยเงื่อนไข
มาสก์บูลีนบน DataFrame
กรองแถวด้วยเงื่อนไข เป็นบทเรียน Data Science Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Data Science Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
From Columns to Rows
Picking columns trims width; filtering rows trims length. Now you keep only the records that match a condition you care about. 🔍
A Condition Makes a Mask
Write a comparison on a column and pandas returns a boolean mask: one True or False per row, telling you which records pass.
mask = df["age"] > 30
print(mask.head())Apply the Mask
Put the mask inside brackets and pandas keeps only the True rows. The result is a smaller DataFrame with the same columns.
adults = df[df["age"] > 30]Equality Filters
Use double equals to match a value exactly. Remember that a single equals would try to assign, not compare.
df[df["city"] == "Paris"]Not Equal and Comparisons
You can filter with the full set of comparisons: not-equal, greater-equal, and less-equal all work on a column.
df[df["score"] >= 80]
df[df["city"] != "Paris"]Filter on Text
String columns support a str accessor. Use str.contains to keep rows whose text includes a pattern.
df[df["email"].str.contains("@gmail")]Filter With between
For a numeric range, between reads more clearly than two separate comparisons and includes both ends by default.
df[df["age"].between(18, 65)]Keep Selected Columns Too
Combine row filtering with loc to grab matching rows and chosen columns in one readable line.
df.loc[df["age"] > 30, ["name", "age"]]The Mask Is Reusable
A mask is just a Series, so you can store it and reuse it across different selections without rewriting the condition.
senior = df["age"] > 60
df[senior]
df.loc[senior, "name"]Count the Matches
Because True counts as one, calling sum on a mask tells you how many rows pass before you even filter.
(df["age"] > 30).sum()Filtering Does Not Change df
Filtering returns a new DataFrame and leaves the original untouched. Assign the result to a variable to keep it.
young = df[df["age"] < 30]
# df itself is unchangedQuick Check
Recall how a comparison on a column behaves.
Recap: Filtering Rows
A condition builds a boolean mask, brackets apply it, and loc adds column selection. You can now keep exactly the records that matter. ✅
คำถามที่พบบ่อย
บทเรียน “กรองแถวด้วยเงื่อนไข” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “กรองแถวด้วยเงื่อนไข” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Data Science Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “กรองแถวด้วยเงื่อนไข”
มาสก์บูลีนบน DataFrame คุณปฏิบัติ Data Science Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Data Science Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Data Science Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “กรองแถวด้วยเงื่อนไข” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Data Science Academy นี้ได้ไหม
ได้ บทเรียน Data Science Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เลือกคอลัมน์ด้วยชื่อ
- กรองแถวด้วยเงื่อนไข
- รวมตัวกรองด้วย AND และ OR
- query และ isin สำหรับตัวกรองที่อ่านง่าย