7
3

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 5 years have passed since last update.

pandas.DataFrameで、MultiIndexのカラムをフラットな形にrenameするする方法

Last updated at Posted at 2019-01-23

サンプルデータのロード

seabornのload_datasetで、pandas.DataFrame型のデータを取得

import pandas as pd
import seaborn as sns
df = sns.load_dataset("iris")
df.head(3)
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa

やりたかったこと

あるカテゴリでGroupbyをしてsumとmeanを取った後、カラム名をフラットな形に変更したかった

df.groupby("species")[["sepal_length", "sepal_width"]].agg(["sum", "mean"])
sepal_length sepal_width
sum mean sum mean
species
setosa 250.3 5.006 171.4 3.428
versicolor 296.8 5.936 138.5 2.770
virginica 329.4 6.588 148.7 2.974

以下のようにカラム名を選択して変更しているつもりだけど変更されず…

df.groupby("species")[["sepal_length", "sepal_width"]].agg(["sum", "mean"]).rename(columns={
    ("sepal_length", "sum"): "sepal_length_sum",
    ("sepal_length", "mean"): "sepal_length_mean",
    ("sepal_width", "sum"): "sepal_width_sum",
    ("sepal_width", "mean"): "sepal_width_mean",
})
sepal_length sepal_width
sum mean sum mean
species
setosa 250.3 5.006 171.4 3.428
versicolor 296.8 5.936 138.5 2.770
virginica 329.4 6.588 148.7 2.974

やったこと

集計後にrenameするのではなく、集計のタイミングでカラムに対しての集計内容と結果格納カラムの名前を指定する

df.groupby("species")[["sepal_length", "sepal_width"]].agg({
    "sepal_length": {
        "sepal_length_sum": "sum",
        "sepal_length_mean": "mean",
    },
    "sepal_width": {
        "sepal_width_sum": "sum",
        "sepal_width_mean": "mean",
    },
})
sepal_length_sum sepal_length_mean sepal_width_sum sepal_width_mean
species
setosa 250.3 5.006 171.4 3.428
versicolor 296.8 5.936 138.5 2.770
virginica 329.4 6.588 148.7 2.974

実行時のWarningについて

.../lib/python3.6/site-packages/pandas/core/groupby/groupby.py:4656: FutureWarning: using a dict with renaming is deprecated and will be removed in a future version
  return super(DataFrameGroupBy, self).aggregate(arg, *args, **kwargs)

dictを引数で渡す方式は、いつか廃止される?
Deprecation of relabeling dicts in groupby.agg brings many issues #18366

7
3
0

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
7
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?