read_csv 及其实用选项
分隔符、表头和索引列
read_csv 及其实用选项 是 CoddyKit 上的免费 Data Science Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Data Science Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Data Science Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Data Lives in Files
Real analysis starts with a file on disk. The most common one in data science is the humble CSV: plain text, one row per line. 📄
One Function to Rule Them
pandas reads a CSV with a single call: read_csv. Hand it a path and you get back a ready-to-use DataFrame.
import pandas as pd
df = pd.read_csv("sales.csv")What Comes Back
read_csv returns a full DataFrame: rows, columns, and an automatic integer index. From here every pandas trick is available to you.
Not Always a Comma
Some files split values with a semicolon or tab instead. The sep option tells pandas exactly which character separates your columns.
df = pd.read_csv("data.csv", sep=";")When There Is No Header
If the first line is data, not column names, pass header=None so pandas does not steal a real row to use as labels.
df = pd.read_csv("raw.csv", header=None)Name Your Columns
Pair header=None with names to supply your own column labels in one clean step. Now every column reads clearly.
df = pd.read_csv("raw.csv", header=None,
names=["date", "item", "price"])Pick the Index Column
The index_col option promotes a column to be the row index, so lookups by that key feel natural later on.
df = pd.read_csv("sales.csv", index_col="order_id")Read Only What You Need
Huge file? The usecols option loads just the columns you list, saving memory and keeping your table focused.
df = pd.read_csv("big.csv",
usecols=["date", "amount"])Peek With nrows
Not sure a file is clean? Use nrows to read a small sample first, inspect it, then load the rest with confidence.
sample = pd.read_csv("big.csv", nrows=5)Decode Properly
Strange symbols in your text usually mean the wrong encoding. Setting it to utf-8 fixes most accented and non-English characters. ✅
df = pd.read_csv("names.csv", encoding="utf-8")Mark Missing as Missing
Files use odd tokens for blanks like NA or n/a. The na_values option turns those into real missing values pandas understands.
df = pd.read_csv("data.csv",
na_values=["NA", "n/a", "-"])Quick Check
Your CSV uses semicolons between values. Which option fixes the parse?
Recap: read_csv Mastered
You can now load any CSV: choose the separator, set headers and names, pick the index, trim columns, and flag missing values. 🎉
常见问题解答
「read_csv 及其实用选项」课时是免费的吗?
是的 — 「read_csv 及其实用选项」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Data Science Academy 课程的其余内容,请升级到 CoddyKit PRO。 Data Science Academy 课程共包含 4 节课。
「read_csv 及其实用选项」这节课中我会学到什么?
分隔符、表头和索引列 你通过在浏览器中直接运行的动手代码来练习 Data Science Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Data Science Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Data Science Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「read_csv 及其实用选项」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Data Science Academy 课中编写并运行代码吗?
能。每节 Data Science Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- read_csv 及其实用选项
- 在 pandas 中打开 Excel 工作表
- 读取时解析日期并设置 dtypes
- 将结果保存为 CSV 和 Excel