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

## サンプルデータのロード
seabornのload_datasetで、pandas.DataFrame型のデータを取得


```python
import pandas as pd
import seaborn as sns
```


```python
df = sns.load_dataset("iris")
df.head(3)
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>sepal_length</th>
      <th>sepal_width</th>
      <th>petal_length</th>
      <th>petal_width</th>
      <th>species</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>5.1</td>
      <td>3.5</td>
      <td>1.4</td>
      <td>0.2</td>
      <td>setosa</td>
    </tr>
    <tr>
      <th>1</th>
      <td>4.9</td>
      <td>3.0</td>
      <td>1.4</td>
      <td>0.2</td>
      <td>setosa</td>
    </tr>
    <tr>
      <th>2</th>
      <td>4.7</td>
      <td>3.2</td>
      <td>1.3</td>
      <td>0.2</td>
      <td>setosa</td>
    </tr>
  </tbody>
</table>
</div>



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


```python
df.groupby("species")[["sepal_length", "sepal_width"]].agg(["sum", "mean"])
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead tr th {
        text-align: left;
    }

    .dataframe thead tr:last-of-type th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr>
      <th></th>
      <th colspan="2" halign="left">sepal_length</th>
      <th colspan="2" halign="left">sepal_width</th>
    </tr>
    <tr>
      <th></th>
      <th>sum</th>
      <th>mean</th>
      <th>sum</th>
      <th>mean</th>
    </tr>
    <tr>
      <th>species</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>setosa</th>
      <td>250.3</td>
      <td>5.006</td>
      <td>171.4</td>
      <td>3.428</td>
    </tr>
    <tr>
      <th>versicolor</th>
      <td>296.8</td>
      <td>5.936</td>
      <td>138.5</td>
      <td>2.770</td>
    </tr>
    <tr>
      <th>virginica</th>
      <td>329.4</td>
      <td>6.588</td>
      <td>148.7</td>
      <td>2.974</td>
    </tr>
  </tbody>
</table>
</div>



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


```python
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",
})
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead tr th {
        text-align: left;
    }

    .dataframe thead tr:last-of-type th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr>
      <th></th>
      <th colspan="2" halign="left">sepal_length</th>
      <th colspan="2" halign="left">sepal_width</th>
    </tr>
    <tr>
      <th></th>
      <th>sum</th>
      <th>mean</th>
      <th>sum</th>
      <th>mean</th>
    </tr>
    <tr>
      <th>species</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>setosa</th>
      <td>250.3</td>
      <td>5.006</td>
      <td>171.4</td>
      <td>3.428</td>
    </tr>
    <tr>
      <th>versicolor</th>
      <td>296.8</td>
      <td>5.936</td>
      <td>138.5</td>
      <td>2.770</td>
    </tr>
    <tr>
      <th>virginica</th>
      <td>329.4</td>
      <td>6.588</td>
      <td>148.7</td>
      <td>2.974</td>
    </tr>
  </tbody>
</table>
</div>



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


```python
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",
    },
})
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>sepal_length_sum</th>
      <th>sepal_length_mean</th>
      <th>sepal_width_sum</th>
      <th>sepal_width_mean</th>
    </tr>
    <tr>
      <th>species</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>setosa</th>
      <td>250.3</td>
      <td>5.006</td>
      <td>171.4</td>
      <td>3.428</td>
    </tr>
    <tr>
      <th>versicolor</th>
      <td>296.8</td>
      <td>5.936</td>
      <td>138.5</td>
      <td>2.770</td>
    </tr>
    <tr>
      <th>virginica</th>
      <td>329.4</td>
      <td>6.588</td>
      <td>148.7</td>
      <td>2.974</td>
    </tr>
  </tbody>
</table>
</div>

## 実行時の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](https://github.com/pandas-dev/pandas/issues/18366)