aggで複数の集計を行う
列ごとに異なる関数を適用する
「aggで複数の集計を行う」はCoddyKit上の無料Data Science Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはData Science Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Data Science Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why One Number Is Not Enough
A single mean hides a lot. With agg you can request several summaries at once and see the fuller picture.
Meet agg
The agg method takes a list of functions and runs every one of them on each group in a single pass.
df.groupby("city")["sales"].agg(["mean", "max"])A List of Function Names
Pass plain strings like "sum" or "min", and pandas maps each to its built-in aggregation for you.
g.agg(["sum", "min", "max"])Each Function Becomes a Column
With a list of functions, the result gains one column per function, neatly side by side for comparison.
Different Functions Per Column
Pass a dictionary to apply different aggregations to different columns in the very same call.
df.groupby("city").agg({"sales": "sum", "age": "mean"})Name Your Output Columns
Named aggregation lets you label results clearly, so you never guess what each output column means.
g.agg(total=("sales", "sum"))Use Your Own Function
Any function returning one value works, so a custom lambda can compute a range or anything you like.
g.agg(lambda s: s.max() - s.min())Mixing Built-ins and Custom
Inside one list you can blend string names with your own callables; pandas applies each to every group.
g.agg(["mean", lambda s: s.std()])Aggregating Many Columns at Once
Skip selecting a column and agg runs across every numeric column, giving a wide summary table.
df.groupby("city").agg("mean")Reset the Index to Flatten
Call reset_index afterward to turn the group keys back into normal columns for easy charting or merging.
result.reset_index()Read the Result Carefully
When functions and columns multiply, you get a MultiIndex on columns; read it as (column, function) pairs.
Quick Check
You want sum on one column and mean on another. What do you pass to agg?
Recap: agg Is Your Swiss Knife
You can now stack many summaries, mix built-ins with custom functions, and name outputs, all through one flexible agg call. 🛠️
よくある質問
「aggで複数の集計を行う」レッスンは無料ですか?
はい。「aggで複数の集計を行う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Data Science Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Data Science Academyコースには全4レッスンが含まれています。
「aggで複数の集計を行う」で何を学びますか?
列ごとに異なる関数を適用する ブラウザで直接実行するハンズオンコードでData Science Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Data Science Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのData Science Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「aggで複数の集計を行う」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このData Science Academyレッスンでコードを書いて実行できますか?
はい。すべてのData Science Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。