クロス集計にpivot_tableを使う
2つの軸にまたがって集計する
「クロス集計にpivot_tableを使う」はCoddyKit上の無料Data Science Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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を使う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Data Science Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Data Science Academyコースには全4レッスンが含まれています。
「クロス集計にpivot_tableを使う」で何を学びますか?
2つの軸にまたがって集計する ブラウザで直接実行するハンズオンコードでData Science Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Data Science Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのData Science Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「クロス集計にpivot_tableを使う」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このData Science Academyレッスンでコードを書いて実行できますか?
はい。すべてのData Science Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ワイド型とロング型、Tidyデータ
- クロス集計にpivot_tableを使う
- meltでロング型に変換する
- stack、unstack、MultiIndex