pivot_table สำหรับตารางไขว้
สรุปข้อมูลข้ามสองมิติ
pivot_table สำหรับตารางไขว้ เป็นบทเรียน Data Science Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Data Science Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
From Long Rows to a Grid
When you want a category-by-category summary, pivot_table turns tidy long rows into a clean two-dimensional grid. 🔄
The Three Key Arguments
Every pivot table needs three roles: which column becomes index, which becomes columns, and which values fill the cells.
df.pivot_table(index='region',
columns='month',
values='sales')It Aggregates by Default
If several rows land in the same cell, pivot_table averages them. The default aggfunc is mean, not a plain copy.
Choose Your Aggregation
Swap the math with aggfunc. Use sum for totals, count for tallies, or max to spot the peak value per cell.
df.pivot_table(index='region',
values='sales',
aggfunc='sum')Many Aggregations at Once
Pass a list to aggfunc and get several summaries together. One call can return both the mean and the count side by side.
df.pivot_table(values='sales',
aggfunc=['mean', 'count'])Filling Empty Cells
Missing combinations show up as NaN. Set fill_value to replace those gaps with zero or any sensible default.
df.pivot_table(index='region',
columns='month',
fill_value=0)Add Row and Column Totals
Set margins to True and pivot_table adds an All row and column with grand totals around the edges.
df.pivot_table(index='region',
margins=True)Group by Several Keys
Pass a list to index or columns to nest categories. The result gains a MultiIndex that stacks the groups neatly.
df.pivot_table(index=['region', 'store'],
values='sales')pivot vs pivot_table
Plain pivot needs unique index-column pairs and cannot aggregate. Reach for pivot_table whenever duplicates might appear.
Reading a Cross-Tab
In the result, scan a cell by its row label and column label to read that exact category combination at a glance.
Back to a Flat Table
A pivot result is still a DataFrame. Call reset_index to flatten its labels back into ordinary columns for export.
summary.reset_index()Quick Check
Let us check your pivot_table reflexes.
Recap: Cross-Tabs Made Easy
You built grids with index, columns, values, and aggfunc. Next you will reverse course and melt wide data back to long. 🎯
คำถามที่พบบ่อย
บทเรียน “pivot_table สำหรับตารางไขว้” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “pivot_table สำหรับตารางไขว้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Data Science Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “pivot_table สำหรับตารางไขว้”
สรุปข้อมูลข้ามสองมิติ คุณปฏิบัติ Data Science Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Data Science Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Data Science Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “pivot_table สำหรับตารางไขว้” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Data Science Academy นี้ได้ไหม
ได้ บทเรียน Data Science Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แบบกว้างกับแบบยาว และข้อมูลเป็นระเบียบ
- pivot_table สำหรับตารางไขว้
- melt เพื่อเปลี่ยนเป็นรูปแบบยาว
- stack, unstack และ MultiIndex