0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pandasのgroupbyで階層構造を解消する

Posted at

環境

Pandasのバージョンは1.1.3を使用

やりたいこと

DataFrameをgroupbyで集約したときに、以下のようにindexとcolumnに階層構造ができてしまう

import pandas as pd
# データの設定
A = ['a', 'a', 'a', 'b', 'b']
B = ['c', 'd', 'c', 'd', 'c']
X = [1, 2, 3, 4, 5]
df = pd.DataFrame({'A':A, 'B':B, 'X':X})
df.groupby(['A', 'B']).agg({'X': {'mean', 'count'}})

今後の取り扱いのために全部バラバラな行や列になっていてほしい

行の解消:.reset_index()

行側の階層構造はgroupbyの後ろにreset_index()をつけて解消することができる

df.groupby(['A', 'B']).agg({'X': {'mean', 'count'}}).reset_index()

列の解消:pd.NamedAgg()を使う

列側の階層構造を解消するにはagg内を変更する
はじめに以下を実行したらエラーが出た


df.groupby(['A', 'B']).agg({'X': {'X_mean':'mean', 'X_count':'count'}})

# -->error
# nested renamer is not supported

どうやらdict型でrename指定して渡すことが出来なくなってしまっているらしいので、別の方法を試した

df.groupby(['A', 'B']).agg(X_mean=pd.NamedAgg(column='X', aggfunc='mean'),
                           X_count=pd.NamedAgg(column='X', aggfunc='count'))

これX_meanとX_countという列にわけることができた!

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?