0Pricing
Data Science Academy · レッスン

複数のキーでグループ化する

多段階のグループ化と結果のインデックス

「複数のキーでグループ化する」はCoddyKit上の無料Data Science Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはData Science Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Data Science Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Group on More Than One Column

Pass a list of keys to groupby and pandas forms a group for every unique combination of those columns.

df.groupby(["city", "year"])

Every Combo Is a Group

Two cities and three years can yield up to six groups, one per existing combination found in your data.

Aggregate Across Keys

Aggregation works exactly the same; the math just runs inside each combination instead of a single key.

df.groupby(["city", "year"])["sales"].sum()

Meet the MultiIndex

Grouping on several keys gives the result a MultiIndex, with one index level for each grouping column.

Read a MultiIndex Result

Each output row is labeled by a tuple like (city, year), so the index tells you exactly which group it is.

Select From a MultiIndex

Use loc with a tuple to pull one specific combination straight out of the grouped result.

result.loc[("Paris", 2024)]

Flatten With reset_index

Prefer flat columns? reset_index turns each index level back into an ordinary column you can filter.

result.reset_index()

Keep Empty Groups Out

By default pandas skips combinations with no rows, so your result shows only combinations that actually occur.

Order of Keys Matters

The key order you list sets the index level order, which changes how the result sorts and reads.

df.groupby(["year", "city"]).size()

Unstack for a Cross-Tab

Call unstack to lift one key into columns, turning a long grouped result into a readable grid.

result.unstack()

When to Use Several Keys

Reach for multi-key grouping whenever a question mentions two dimensions, like sales by city and by month.

Quick Check

You group by ["city", "year"]. What labels each result row?

Recap: Two Dimensions, One groupby

You can now slice data along several keys at once, read the MultiIndex, and reshape it for clear cross-tabs. 📊

よくある質問

「複数のキーでグループ化する」レッスンは無料ですか?

はい。「複数のキーでグループ化する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Data Science Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Data Science Academyコースには全4レッスンが含まれています。

「複数のキーでグループ化する」で何を学びますか?

多段階のグループ化と結果のインデックス ブラウザで直接実行するハンズオンコードでData Science Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Data Science Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのData Science Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「複数のキーでグループ化する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このData Science Academyレッスンでコードを書いて実行できますか?

はい。すべてのData Science Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Split-Apply-Combineを理解する
  2. aggで複数の集計を行う
  3. 複数のキーでグループ化する
  4. グループ単位の特徴量にtransformを使う
← Data Science Academyに戻る