melt เพื่อเปลี่ยนเป็นรูปแบบยาว
เปลี่ยนคอลัมน์ให้เป็นแถว
melt เพื่อเปลี่ยนเป็นรูปแบบยาว เป็นบทเรียน Data Science Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Data Science Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Opposite of Pivot
When columns are really values in disguise, melt unpivots them into rows, turning a wide table into tidy long form. 🫠
What melt Produces
Melt creates two new columns: one holding the old column names, one holding their values. Every cell becomes its own row.
pd.melt(df)Keep Some Columns Fixed
Use id_vars to list columns that should stay put, like an id or name. Those repeat down the new long table.
df.melt(id_vars='name')Choose What to Unpivot
Set value_vars to melt only specific columns. Anything left out and not in id_vars is simply dropped from the result.
df.melt(id_vars='name',
value_vars=['Jan', 'Feb'])Name Your New Columns
The defaults are variable and value. Pass var_name and value_name to give them meaningful labels like month and sales.
df.melt(var_name='month',
value_name='sales')Why Melt Before Plotting
Libraries like seaborn want long input. A quick melt lets you map one column to color or facet and plot many series at once.
Melt Then GroupBy
Once data is long, summaries get simple. After melt you can groupby the new variable column and aggregate every series uniformly.
df.melt(id_vars='name').groupby('variable')Melt and Pivot Are Inverses
Melt and pivot undo each other. Go wide to long with melt, then pivot back, and you recover the original layout.
Watch the Row Explosion
Melting multiplies rows: ten columns over five rows becomes fifty long rows. Expect the row count to grow fast.
wide_to_long for Patterns
For columns sharing a prefix like sales_2021, wide_to_long melts them while parsing the year out of each name.
A Clean Melt Recipe
A reliable pattern: name your id columns, name the value columns, and label the output. That gives a fully tidy long table.
df.melt(id_vars='name',
var_name='month',
value_name='sales')Quick Check
Quick check on melting wide tables.
Recap: Going Long
You used melt with id_vars, value_vars, and custom names to tidy wide data. Next: stack and unstack on a MultiIndex. 🎯
คำถามที่พบบ่อย
บทเรียน “melt เพื่อเปลี่ยนเป็นรูปแบบยาว” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “melt เพื่อเปลี่ยนเป็นรูปแบบยาว” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Data Science Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “melt เพื่อเปลี่ยนเป็นรูปแบบยาว”
เปลี่ยนคอลัมน์ให้เป็นแถว คุณปฏิบัติ Data Science Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Data Science Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Data Science Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “melt เพื่อเปลี่ยนเป็นรูปแบบยาว” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Data Science Academy นี้ได้ไหม
ได้ บทเรียน Data Science Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แบบกว้างกับแบบยาว และข้อมูลเป็นระเบียบ
- pivot_table สำหรับตารางไขว้
- melt เพื่อเปลี่ยนเป็นรูปแบบยาว
- stack, unstack และ MultiIndex