0Pricing
Data Science Academy · บทเรียน

สร้าง DataFrame ตั้งแต่ต้น

จากดิกชันนารี ลิสต์ และอาร์เรย์

สร้าง DataFrame ตั้งแต่ต้น เป็นบทเรียน Data Science Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Data Science Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Build Your Own

Before loading real files, building a small DataFrame by hand lets you test ideas and learn the structure with zero setup. 🧪

Import pandas First

Every project starts the same way: import pandas under the nickname pd. That short alias is a universal convention you will see everywhere.

import pandas as pd

From a Dictionary of Columns

The most common recipe is a dict where each key is a column name and each value is a list of that column's data.

df = pd.DataFrame({"name": ["Ada", "Lin"], "age": [36, 41]})

Keys Become Headers

With the dict approach, the dictionary keys turn into your column headers automatically. Naming columns is as easy as naming keys.

From a List of Rows

You can also pass a list of rows and supply the column names separately. Handy when your data already arrives row by row.

pd.DataFrame([["Ada", 36], ["Lin", 41]], columns=["name", "age"])

From a List of Dicts

A list of dicts works too, with one dict per row. Missing keys simply become blanks, which is great for uneven records.

pd.DataFrame([{"name": "Ada"}, {"name": "Lin", "age": 41}])

From a NumPy Array

Have a NumPy array of numbers? Pass it straight in and add column names. This bridges fast NumPy math with labeled tables.

pd.DataFrame(arr, columns=["x", "y"])

Set the Index on Creation

Pass an index argument to label your rows the moment you build the frame, instead of relying on default 0, 1, 2 numbers.

pd.DataFrame(data, index=["r1", "r2"])

Control the Column Order

Want columns in a specific order? Pass a columns list and pandas arranges them exactly as you list, ignoring dict ordering quirks.

pd.DataFrame(data, columns=["age", "name"])

Lengths Must Match

Every column you provide must have the same length. Mismatched list sizes raise an error, so keep your columns even.

Peek at What You Built

After building, glance at dtypes to confirm pandas inferred sensible types like int for ages and object for names.

df.dtypes

Quick Check

Pick the building block behind the most common DataFrame recipe.

Recap: Many Ways In

Dicts, lists, list-of-dicts, or NumPy arrays all become a DataFrame. Pick whatever matches the shape of your data. 🛠️

คำถามที่พบบ่อย

บทเรียน “สร้าง DataFrame ตั้งแต่ต้น” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “สร้าง DataFrame ตั้งแต่ต้น” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 บทเรียน

บทเรียน “สร้าง DataFrame ตั้งแต่ต้น” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Data Science Academy นี้ได้ไหม

ได้ บทเรียน Data Science Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แถว คอลัมน์ และดัชนี
  2. สร้าง DataFrame ตั้งแต่ต้น
  3. head, info และ describe
  4. เพิ่ม เปลี่ยนชื่อ และลบคอลัมน์
← กลับไปที่ Data Science Academy