DataFrameの値をGroup毎に計算する時に，group keyが消えてしまうのを防ぐ

# はじめに
こういうデータがあった時に
```python
tmp = pd.DataFrame(dict(key=np.arange(10)//5, time=np.arange(10)%5, val1=np.random.randn(10),val2=np.random.randn(10)))
tmp
```

<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>key</th>
      <th>time</th>
      <th>val1</th>
      <th>val2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0</td>
      <td>0</td>
      <td>-1.152069</td>
      <td>0.788045</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0</td>
      <td>1</td>
      <td>0.133470</td>
      <td>0.347221</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0</td>
      <td>2</td>
      <td>0.483643</td>
      <td>0.487755</td>
    </tr>
    <tr>
      <th>3</th>
      <td>0</td>
      <td>3</td>
      <td>1.691687</td>
      <td>1.426061</td>
    </tr>
    <tr>
      <th>4</th>
      <td>0</td>
      <td>4</td>
      <td>-0.575070</td>
      <td>0.050923</td>
    </tr>
    <tr>
      <th>5</th>
      <td>1</td>
      <td>0</td>
      <td>-2.627809</td>
      <td>-0.251222</td>
    </tr>
    <tr>
      <th>6</th>
      <td>1</td>
      <td>1</td>
      <td>0.668707</td>
      <td>-1.490587</td>
    </tr>
    <tr>
      <th>7</th>
      <td>1</td>
      <td>2</td>
      <td>0.674961</td>
      <td>0.623323</td>
    </tr>
    <tr>
      <th>8</th>
      <td>1</td>
      <td>3</td>
      <td>-1.788848</td>
      <td>-0.915043</td>
    </tr>
    <tr>
      <th>9</th>
      <td>1</td>
      <td>4</td>
      <td>-1.027477</td>
      <td>0.880744</td>
    </tr>
  </tbody>
</table>
</div>

以下のように``'val1','val2'``に対して
Group毎に処理をしようとするとGroupkeyが消えてしまう
```python
tmp.groupby(['key'])[['val1','val2']].transform(lambda s: s)
```
<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>val1</th>
      <th>val2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-1.152069</td>
      <td>0.788045</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0.133470</td>
      <td>0.347221</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0.483643</td>
      <td>0.487755</td>
    </tr>
    <tr>
      <th>3</th>
      <td>1.691687</td>
      <td>1.426061</td>
    </tr>
    <tr>
      <th>4</th>
      <td>-0.575070</td>
      <td>0.050923</td>
    </tr>
    <tr>
      <th>5</th>
      <td>-2.627809</td>
      <td>-0.251222</td>
    </tr>
    <tr>
      <th>6</th>
      <td>0.668707</td>
      <td>-1.490587</td>
    </tr>
    <tr>
      <th>7</th>
      <td>0.674961</td>
      <td>0.623323</td>
    </tr>
    <tr>
      <th>8</th>
      <td>-1.788848</td>
      <td>-0.915043</td>
    </tr>
    <tr>
      <th>9</th>
      <td>-1.027477</td>
      <td>0.880744</td>
    </tr>
  </tbody>
</table>
</div>

# 解決方法
消えてほしくないlabelはindexにしておく
```python
tmp.set_index(['key','time']).groupby(['key'])[['val1','val2']].transform(lambda s: s)
```
<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></th>
      <th>val1</th>
      <th>val2</th>
    </tr>
    <tr>
      <th>key</th>
      <th>time</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th rowspan="5" valign="top">0</th>
      <th>0</th>
      <td>-1.152069</td>
      <td>0.788045</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0.133470</td>
      <td>0.347221</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0.483643</td>
      <td>0.487755</td>
    </tr>
    <tr>
      <th>3</th>
      <td>1.691687</td>
      <td>1.426061</td>
    </tr>
    <tr>
      <th>4</th>
      <td>-0.575070</td>
      <td>0.050923</td>
    </tr>
    <tr>
      <th rowspan="5" valign="top">1</th>
      <th>0</th>
      <td>-2.627809</td>
      <td>-0.251222</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0.668707</td>
      <td>-1.490587</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0.674961</td>
      <td>0.623323</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-1.788848</td>
      <td>-0.915043</td>
    </tr>
    <tr>
      <th>4</th>
      <td>-1.027477</td>
      <td>0.880744</td>
    </tr>
  </tbody>
</table>
</div>

# 使用例
- key毎にnormalize
```python
tmp.set_index(['key','time']).groupby(['key'])[['val1','val2']].transform(lambda s: (s-s.mean())/s.std())
```
<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></th>
      <th>val1</th>
      <th>val2</th>
    </tr>
    <tr>
      <th>key</th>
      <th>time</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th rowspan="5" valign="top">0</th>
      <th>0</th>
      <td>-1.169663</td>
      <td>0.321366</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0.015803</td>
      <td>-0.521662</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0.338717</td>
      <td>-0.252906</td>
    </tr>
    <tr>
      <th>3</th>
      <td>1.452722</td>
      <td>1.541503</td>
    </tr>
    <tr>
      <th>4</th>
      <td>-0.637580</td>
      <td>-1.088302</td>
    </tr>
    <tr>
      <th rowspan="5" valign="top">1</th>
      <th>0</th>
      <td>-1.225673</td>
      <td>-0.020611</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1.009441</td>
      <td>-1.256779</td>
    </tr>
    <tr>
      <th>2</th>
      <td>1.013681</td>
      <td>0.851676</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.656838</td>
      <td>-0.682719</td>
    </tr>
    <tr>
      <th>4</th>
      <td>-0.140611</td>
      <td>1.108433</td>
    </tr>
  </tbody>
</table>
</div>

- key毎にdiff
```python
tmp.set_index(['key','time']).groupby(['key'])[['val1','val2']].diff()
```




<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></th>
      <th>val1</th>
      <th>val2</th>
    </tr>
    <tr>
      <th>key</th>
      <th>time</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th rowspan="5" valign="top">0</th>
      <th>0</th>
      <td>NaN</td>
      <td>NaN</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1.285538</td>
      <td>-0.440823</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0.350173</td>
      <td>0.140534</td>
    </tr>
    <tr>
      <th>3</th>
      <td>1.208045</td>
      <td>0.938305</td>
    </tr>
    <tr>
      <th>4</th>
      <td>-2.266757</td>
      <td>-1.375138</td>
    </tr>
    <tr>
      <th rowspan="5" valign="top">1</th>
      <th>0</th>
      <td>NaN</td>
      <td>NaN</td>
    </tr>
    <tr>
      <th>1</th>
      <td>3.296516</td>
      <td>-1.239366</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0.006253</td>
      <td>2.113910</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-2.463809</td>
      <td>-1.538365</td>
    </tr>
    <tr>
      <th>4</th>
      <td>0.761371</td>
      <td>1.795787</td>
    </tr>
  </tbody>
</table>
</div>

以上