详解拆分—应用—合并
每个 groupby 背后的模型
详解拆分—应用—合并 是 CoddyKit 上的免费 Data Science Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Data Science Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Data Science Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
One Idea, Three Steps
Every groupby follows one rhythm: split the rows into groups, apply a calculation to each, then combine the answers back together. 🔁
Split: Cut by a Key
The split step partitions rows by a key column, so all rows sharing the same value land in the same little group.
groups = df.groupby("city")Apply: Do Work Per Group
In the apply step, the same function runs on each group on its own, never mixing one group with another.
df.groupby("city")["sales"].mean()Combine: Stitch Results
The combine step glues each group result into one tidy output, indexed by the group keys you split on.
groupby Is Lazy
Calling groupby alone does almost nothing; it just remembers the plan. The real work waits until you add an aggregation.
g = df.groupby("city") # no math yetThe Group Key Becomes the Index
After aggregating, your group key moves into the result index, so each unique value labels one output row.
Pick a Column to Aggregate
Select a column after grouping to focus the math. Here you ask for the average sales within each city.
df.groupby("city")["sales"].mean()size Counts Rows Per Group
Use size when you just want how many rows fell into each group, including any missing values.
df.groupby("city").size()Iterating Over Groups
You can loop a groupby to inspect it: each turn hands you the group name and the matching sub-table.
for name, part in df.groupby("city"):
print(name, len(part))Why It Beats Manual Loops
Split-apply-combine replaces slow hand-written loops with one fast, readable line that pandas optimizes for you. ⚡
A Tiny End-to-End Example
This single line splits by region, averages each group, and combines the result, all in one readable expression.
df.groupby("region")["revenue"].mean()Quick Check
Which step actually does the calculation?
Recap: The groupby Rhythm
You learned the heartbeat of grouping: split, apply, combine. Master this rhythm and every aggregation later feels natural. 🎯
常见问题解答
「详解拆分—应用—合并」课时是免费的吗?
是的 — 「详解拆分—应用—合并」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Data Science Academy 课程的其余内容,请升级到 CoddyKit PRO。 Data Science Academy 课程共包含 4 节课。
「详解拆分—应用—合并」这节课中我会学到什么?
每个 groupby 背后的模型 你通过在浏览器中直接运行的动手代码来练习 Data Science Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Data Science Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Data Science Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「详解拆分—应用—合并」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Data Science Academy 课中编写并运行代码吗?
能。每节 Data Science Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 详解拆分—应用—合并
- 使用 agg 执行多种聚合
- 按多个键分组
- 使用 transform 创建分组特征