This request has already been treated.

  1. mizoe@github
Changes in body
Source | HTML | Preview
@@ -1,2635 +1,2636 @@
# 伝えたい事
R言語における`magrittr`そして`dplyr`によるデータハンドリングはとても心地が良いです.
データの処理のパイプラインが実に美しいです.
一方,いろんな方が書いた`pandas`におけるデータを処理のコードを見ていると
```python
df = df[df.a >0]
df = ...
```
と再帰代入を何度も繰り返していて美しくない.ややこしいことをいうと参照透過性を損う.
でも!!そんな再帰代入を繰り返さなくてもいい,pandasでもメソッドチェーンでデータ処理ができるってのが今日伝えたいことです.
とくにぜひ皆さんに使って欲しいmethodたち
* `pipe`
* `assign`
* `reset_index`
* `set_index`
* `group_by`
* `pivot`
* `stack`
これらを頭に叩き込んで使ってもらうべく紹介するってのがこの記事の趣旨です.notebookからのmarkdown吐き出しなので多少の崩れはご容赦ください.
```python
import numpy as np
import pandas as pd
```
# 今回使用するデータの生成
乱数で生成したこのデータが今日の相棒です.
```python
df = pd.DataFrame(np.random.randn(20,5), columns=list('ABCDE'))
df
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>-0.489997</td>
<td>0.169059</td>
<td>1.743541</td>
<td>-0.432755</td>
<td>-0.595007</td>
</tr>
<tr>
<th>1</th>
<td>-1.530363</td>
<td>0.711810</td>
<td>-0.382714</td>
<td>-0.139916</td>
<td>-1.734980</td>
</tr>
<tr>
<th>2</th>
<td>-0.334975</td>
<td>1.045337</td>
<td>0.757287</td>
<td>1.351123</td>
<td>0.557953</td>
</tr>
<tr>
<th>3</th>
<td>0.744929</td>
<td>0.484988</td>
<td>0.429293</td>
<td>-3.272832</td>
<td>0.261902</td>
</tr>
<tr>
<th>4</th>
<td>0.407953</td>
<td>0.403938</td>
<td>0.265210</td>
<td>0.818324</td>
<td>-0.657293</td>
</tr>
<tr>
<th>5</th>
<td>-0.208507</td>
<td>-0.144381</td>
<td>-0.553875</td>
<td>-3.878549</td>
<td>-0.480763</td>
</tr>
<tr>
<th>6</th>
<td>0.019297</td>
<td>1.503824</td>
<td>0.680546</td>
<td>1.415549</td>
<td>-1.554857</td>
</tr>
<tr>
<th>7</th>
<td>1.544483</td>
<td>-0.209555</td>
<td>-0.446111</td>
<td>-0.785419</td>
<td>0.220515</td>
</tr>
<tr>
<th>8</th>
<td>-1.312732</td>
<td>1.321994</td>
<td>0.320443</td>
<td>-2.245312</td>
<td>-0.409711</td>
</tr>
<tr>
<th>9</th>
<td>-0.699864</td>
<td>0.050989</td>
<td>-0.189841</td>
<td>0.269374</td>
<td>-1.098471</td>
</tr>
<tr>
<th>10</th>
<td>1.120992</td>
<td>0.856170</td>
<td>0.223470</td>
<td>-0.822884</td>
<td>-0.140143</td>
</tr>
<tr>
<th>11</th>
<td>-1.173929</td>
<td>0.155096</td>
<td>-0.589718</td>
<td>0.049350</td>
<td>-0.001723</td>
</tr>
<tr>
<th>12</th>
<td>-0.295916</td>
<td>1.322868</td>
<td>-0.731492</td>
<td>-1.995471</td>
<td>-0.991209</td>
</tr>
<tr>
<th>13</th>
<td>1.804382</td>
<td>0.626075</td>
<td>-0.383017</td>
<td>1.078767</td>
<td>1.487689</td>
</tr>
<tr>
<th>14</th>
<td>-0.345145</td>
<td>-0.318551</td>
<td>-1.244325</td>
<td>0.541235</td>
<td>-0.261979</td>
</tr>
<tr>
<th>15</th>
<td>-1.335849</td>
<td>0.355642</td>
<td>2.910935</td>
<td>0.734953</td>
<td>-0.342311</td>
</tr>
<tr>
<th>16</th>
<td>1.148712</td>
<td>-0.285870</td>
<td>0.162114</td>
<td>-0.547444</td>
<td>2.438300</td>
</tr>
<tr>
<th>17</th>
<td>0.073855</td>
<td>-0.402590</td>
<td>-1.529420</td>
<td>-1.099915</td>
<td>-0.197386</td>
</tr>
<tr>
<th>18</th>
<td>-0.445434</td>
<td>0.468738</td>
<td>0.097532</td>
<td>1.569179</td>
<td>-0.393965</td>
</tr>
<tr>
<th>19</th>
<td>-1.885451</td>
<td>1.426260</td>
<td>-1.775148</td>
<td>0.993585</td>
<td>0.834851</td>
</tr>
</tbody>
</table>
</div>
# lambda式
pythonには簡易な関数リテラルがあります.使い方は
```python
lambda 仮引数: 返り値
```
です.
## 例:
```python
func = lambda x: x**2
func(2)
```
4
```python
func(3)
```
9
# apply
各列(`axis=0`)または各行(`axis=1`)に対して関数を適用するメソッドです.返り値は`pandas.Series`
## 例:
```python
df.apply(lambda x: sum(x), axis=1)
```
0 0.394842
1 -3.076163
2 3.376725
3 -1.351720
4 1.238132
5 -5.266074
6 2.064360
7 0.323913
8 -2.325318
9 -1.667813
10 1.237605
11 -1.560923
12 -2.691220
13 4.613897
14 -1.628766
15 2.323369
16 2.915812
17 -3.155455
18 1.296050
19 -0.405903
dtype: float64
# assign
新たな列を作成するメソッドです.
返り値は同じく`pandas.DataFrame`
与える関数は`pandas.DataFrame`をうけとって`pandas.Series`を返すものであれば自由です.
## 例:
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>round_A</th>
<th>round_B</th>
<th>total</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>-0.489997</td>
<td>0.169059</td>
<td>1.743541</td>
<td>-0.432755</td>
<td>-0.595007</td>
<td>-0</td>
<td>0</td>
<td>0.394842</td>
</tr>
<tr>
<th>1</th>
<td>-1.530363</td>
<td>0.711810</td>
<td>-0.382714</td>
<td>-0.139916</td>
<td>-1.734980</td>
<td>-2</td>
<td>1</td>
<td>-3.076163</td>
</tr>
<tr>
<th>2</th>
<td>-0.334975</td>
<td>1.045337</td>
<td>0.757287</td>
<td>1.351123</td>
<td>0.557953</td>
<td>-0</td>
<td>1</td>
<td>3.376725</td>
</tr>
<tr>
<th>3</th>
<td>0.744929</td>
<td>0.484988</td>
<td>0.429293</td>
<td>-3.272832</td>
<td>0.261902</td>
<td>1</td>
<td>0</td>
<td>-1.351720</td>
</tr>
<tr>
<th>4</th>
<td>0.407953</td>
<td>0.403938</td>
<td>0.265210</td>
<td>0.818324</td>
<td>-0.657293</td>
<td>0</td>
<td>0</td>
<td>1.238132</td>
</tr>
<tr>
<th>5</th>
<td>-0.208507</td>
<td>-0.144381</td>
<td>-0.553875</td>
<td>-3.878549</td>
<td>-0.480763</td>
<td>-0</td>
<td>-0</td>
<td>-5.266074</td>
</tr>
<tr>
<th>6</th>
<td>0.019297</td>
<td>1.503824</td>
<td>0.680546</td>
<td>1.415549</td>
<td>-1.554857</td>
<td>0</td>
<td>2</td>
<td>2.064360</td>
</tr>
<tr>
<th>7</th>
<td>1.544483</td>
<td>-0.209555</td>
<td>-0.446111</td>
<td>-0.785419</td>
<td>0.220515</td>
<td>2</td>
<td>-0</td>
<td>0.323913</td>
</tr>
<tr>
<th>8</th>
<td>-1.312732</td>
<td>1.321994</td>
<td>0.320443</td>
<td>-2.245312</td>
<td>-0.409711</td>
<td>-1</td>
<td>1</td>
<td>-2.325318</td>
</tr>
<tr>
<th>9</th>
<td>-0.699864</td>
<td>0.050989</td>
<td>-0.189841</td>
<td>0.269374</td>
<td>-1.098471</td>
<td>-1</td>
<td>0</td>
<td>-1.667813</td>
</tr>
<tr>
<th>10</th>
<td>1.120992</td>
<td>0.856170</td>
<td>0.223470</td>
<td>-0.822884</td>
<td>-0.140143</td>
<td>1</td>
<td>1</td>
<td>1.237605</td>
</tr>
<tr>
<th>11</th>
<td>-1.173929</td>
<td>0.155096</td>
<td>-0.589718</td>
<td>0.049350</td>
<td>-0.001723</td>
<td>-1</td>
<td>0</td>
<td>-1.560923</td>
</tr>
<tr>
<th>12</th>
<td>-0.295916</td>
<td>1.322868</td>
<td>-0.731492</td>
<td>-1.995471</td>
<td>-0.991209</td>
<td>-0</td>
<td>1</td>
<td>-2.691220</td>
</tr>
<tr>
<th>13</th>
<td>1.804382</td>
<td>0.626075</td>
<td>-0.383017</td>
<td>1.078767</td>
<td>1.487689</td>
<td>2</td>
<td>1</td>
<td>4.613897</td>
</tr>
<tr>
<th>14</th>
<td>-0.345145</td>
<td>-0.318551</td>
<td>-1.244325</td>
<td>0.541235</td>
<td>-0.261979</td>
<td>-0</td>
<td>-0</td>
<td>-1.628766</td>
</tr>
<tr>
<th>15</th>
<td>-1.335849</td>
<td>0.355642</td>
<td>2.910935</td>
<td>0.734953</td>
<td>-0.342311</td>
<td>-1</td>
<td>0</td>
<td>2.323369</td>
</tr>
<tr>
<th>16</th>
<td>1.148712</td>
<td>-0.285870</td>
<td>0.162114</td>
<td>-0.547444</td>
<td>2.438300</td>
<td>1</td>
<td>-0</td>
<td>2.915812</td>
</tr>
<tr>
<th>17</th>
<td>0.073855</td>
<td>-0.402590</td>
<td>-1.529420</td>
<td>-1.099915</td>
<td>-0.197386</td>
<td>0</td>
<td>-0</td>
<td>-3.155455</td>
</tr>
<tr>
<th>18</th>
<td>-0.445434</td>
<td>0.468738</td>
<td>0.097532</td>
<td>1.569179</td>
<td>-0.393965</td>
<td>-0</td>
<td>0</td>
<td>1.296050</td>
</tr>
<tr>
<th>19</th>
<td>-1.885451</td>
<td>1.426260</td>
<td>-1.775148</td>
<td>0.993585</td>
<td>0.834851</td>
<td>-2</td>
<td>1</td>
<td>-0.405903</td>
</tr>
</tbody>
</table>
</div>
# 列の選択
sqlで言うところのSelectは
+
```python
df[['A', 'B']]
```
で実現できます
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']]
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>round_A</th>
<th>round_B</th>
<th>total</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>-0</td>
<td>0</td>
<td>0.394842</td>
</tr>
<tr>
<th>1</th>
<td>-2</td>
<td>1</td>
<td>-3.076163</td>
</tr>
<tr>
<th>2</th>
<td>-0</td>
<td>1</td>
<td>3.376725</td>
</tr>
<tr>
<th>3</th>
<td>1</td>
<td>0</td>
<td>-1.351720</td>
</tr>
<tr>
<th>4</th>
<td>0</td>
<td>0</td>
<td>1.238132</td>
</tr>
<tr>
<th>5</th>
<td>-0</td>
<td>-0</td>
<td>-5.266074</td>
</tr>
<tr>
<th>6</th>
<td>0</td>
<td>2</td>
<td>2.064360</td>
</tr>
<tr>
<th>7</th>
<td>2</td>
<td>-0</td>
<td>0.323913</td>
</tr>
<tr>
<th>8</th>
<td>-1</td>
<td>1</td>
<td>-2.325318</td>
</tr>
<tr>
<th>9</th>
<td>-1</td>
<td>0</td>
<td>-1.667813</td>
</tr>
<tr>
<th>10</th>
<td>1</td>
<td>1</td>
<td>1.237605</td>
</tr>
<tr>
<th>11</th>
<td>-1</td>
<td>0</td>
<td>-1.560923</td>
</tr>
<tr>
<th>12</th>
<td>-0</td>
<td>1</td>
<td>-2.691220</td>
</tr>
<tr>
<th>13</th>
<td>2</td>
<td>1</td>
<td>4.613897</td>
</tr>
<tr>
<th>14</th>
<td>-0</td>
<td>-0</td>
<td>-1.628766</td>
</tr>
<tr>
<th>15</th>
<td>-1</td>
<td>0</td>
<td>2.323369</td>
</tr>
<tr>
<th>16</th>
<td>1</td>
<td>-0</td>
<td>2.915812</td>
</tr>
<tr>
<th>17</th>
<td>0</td>
<td>-0</td>
<td>-3.155455</td>
</tr>
<tr>
<th>18</th>
<td>-0</td>
<td>0</td>
<td>1.296050</td>
</tr>
<tr>
<th>19</th>
<td>-2</td>
<td>1</td>
<td>-0.405903</td>
</tr>
</tbody>
</table>
</div>
# rename
列の名前を書き換えたいときは`rename`メソッドを使います
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
})
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>id</th>
<th>key</th>
<th>value</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>-0</td>
<td>0</td>
<td>0.394842</td>
</tr>
<tr>
<th>1</th>
<td>-2</td>
<td>1</td>
<td>-3.076163</td>
</tr>
<tr>
<th>2</th>
<td>-0</td>
<td>1</td>
<td>3.376725</td>
</tr>
<tr>
<th>3</th>
<td>1</td>
<td>0</td>
<td>-1.351720</td>
</tr>
<tr>
<th>4</th>
<td>0</td>
<td>0</td>
<td>1.238132</td>
</tr>
<tr>
<th>5</th>
<td>-0</td>
<td>-0</td>
<td>-5.266074</td>
</tr>
<tr>
<th>6</th>
<td>0</td>
<td>2</td>
<td>2.064360</td>
</tr>
<tr>
<th>7</th>
<td>2</td>
<td>-0</td>
<td>0.323913</td>
</tr>
<tr>
<th>8</th>
<td>-1</td>
<td>1</td>
<td>-2.325318</td>
</tr>
<tr>
<th>9</th>
<td>-1</td>
<td>0</td>
<td>-1.667813</td>
</tr>
<tr>
<th>10</th>
<td>1</td>
<td>1</td>
<td>1.237605</td>
</tr>
<tr>
<th>11</th>
<td>-1</td>
<td>0</td>
<td>-1.560923</td>
</tr>
<tr>
<th>12</th>
<td>-0</td>
<td>1</td>
<td>-2.691220</td>
</tr>
<tr>
<th>13</th>
<td>2</td>
<td>1</td>
<td>4.613897</td>
</tr>
<tr>
<th>14</th>
<td>-0</td>
<td>-0</td>
<td>-1.628766</td>
</tr>
<tr>
<th>15</th>
<td>-1</td>
<td>0</td>
<td>2.323369</td>
</tr>
<tr>
<th>16</th>
<td>1</td>
<td>-0</td>
<td>2.915812</td>
</tr>
<tr>
<th>17</th>
<td>0</td>
<td>-0</td>
<td>-3.155455</td>
</tr>
<tr>
<th>18</th>
<td>-0</td>
<td>0</td>
<td>1.296050</td>
</tr>
<tr>
<th>19</th>
<td>-2</td>
<td>1</td>
<td>-0.405903</td>
</tr>
</tbody>
</table>
</div>
# groupby().`集計関数`()
`max`以外にも`avg`や`sum`など基本的な集計関数が標準で備え付けられています.
返り値には階層的インデックスを持つDataFrameが返ってくるのですがこれが実はpandasのハマりどこだったりします.
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
}).groupby(['id', 'key']).max()
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th></th>
<th>value</th>
</tr>
<tr>
<th>id</th>
<th>key</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>-2</th>
<th>1</th>
<td>-0.405903</td>
</tr>
<tr>
<th rowspan="2" valign="top">-1</th>
<th>0</th>
<td>2.323369</td>
</tr>
<tr>
<th>1</th>
<td>-2.325318</td>
</tr>
<tr>
<th rowspan="3" valign="top">-0</th>
<th>0</th>
<td>1.296050</td>
</tr>
<tr>
<th>1</th>
<td>3.376725</td>
</tr>
<tr>
<th>2</th>
<td>2.064360</td>
</tr>
<tr>
<th rowspan="2" valign="top">1</th>
<th>0</th>
<td>2.915812</td>
</tr>
<tr>
<th>1</th>
<td>1.237605</td>
</tr>
<tr>
<th rowspan="2" valign="top">2</th>
<th>0</th>
<td>0.323913</td>
</tr>
<tr>
<th>1</th>
<td>4.613897</td>
</tr>
</tbody>
</table>
</div>
# reset_indexとset_index
indexをリセットしてデータとして扱い直します.新しいindexには再度通番が振られます
set_indexは指定した列でindexを上書きします.
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
}).groupby(['id', 'key']).max().reset_index()
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>id</th>
<th>key</th>
<th>value</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>-2</td>
<td>1</td>
<td>-0.405903</td>
</tr>
<tr>
<th>1</th>
<td>-1</td>
<td>0</td>
<td>2.323369</td>
</tr>
<tr>
<th>2</th>
<td>-1</td>
<td>1</td>
<td>-2.325318</td>
</tr>
<tr>
<th>3</th>
<td>-0</td>
<td>0</td>
<td>1.296050</td>
</tr>
<tr>
<th>4</th>
<td>-0</td>
<td>1</td>
<td>3.376725</td>
</tr>
<tr>
<th>5</th>
<td>-0</td>
<td>2</td>
<td>2.064360</td>
</tr>
<tr>
<th>6</th>
<td>1</td>
<td>0</td>
<td>2.915812</td>
</tr>
<tr>
<th>7</th>
<td>1</td>
<td>1</td>
<td>1.237605</td>
</tr>
<tr>
<th>8</th>
<td>2</td>
<td>0</td>
<td>0.323913</td>
</tr>
<tr>
<th>9</th>
<td>2</td>
<td>1</td>
<td>4.613897</td>
</tr>
</tbody>
</table>
</div>
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
}).groupby(['id', 'key']).max().reset_index().set_index('id')
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>key</th>
<th>value</th>
</tr>
<tr>
<th>id</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>-2</th>
<td>1</td>
<td>-0.405903</td>
</tr>
<tr>
<th>-1</th>
<td>0</td>
<td>2.323369</td>
</tr>
<tr>
<th>-1</th>
<td>1</td>
<td>-2.325318</td>
</tr>
<tr>
<th>-0</th>
<td>0</td>
<td>1.296050</td>
</tr>
<tr>
<th>-0</th>
<td>1</td>
<td>3.376725</td>
</tr>
<tr>
<th>-0</th>
<td>2</td>
<td>2.064360</td>
</tr>
<tr>
<th>1</th>
<td>0</td>
<td>2.915812</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>1.237605</td>
</tr>
<tr>
<th>2</th>
<td>0</td>
<td>0.323913</td>
</tr>
<tr>
<th>2</th>
<td>1</td>
<td>4.613897</td>
</tr>
</tbody>
</table>
</div>
# pipe
データフレーム全体に対する関数の適用です.とくに
```python
df.pipe(lambda df: df[df.f1 > 0])
```
みたいないわゆるselect-where構文らしきあは頻出のイディオムです.
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
}).groupby(['id', 'key']).max().reset_index().set_index('id').pipe(
lambda df: df[df.value > -2]
)
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>key</th>
<th>value</th>
</tr>
<tr>
<th>id</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>-2</th>
<td>1</td>
<td>-0.405903</td>
</tr>
<tr>
<th>-1</th>
<td>0</td>
<td>2.323369</td>
</tr>
<tr>
<th>-0</th>
<td>0</td>
<td>1.296050</td>
</tr>
<tr>
<th>-0</th>
<td>1</td>
<td>3.376725</td>
</tr>
<tr>
<th>-0</th>
<td>2</td>
<td>2.064360</td>
</tr>
<tr>
<th>1</th>
<td>0</td>
<td>2.915812</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>1.237605</td>
</tr>
<tr>
<th>2</th>
<td>0</td>
<td>0.323913</td>
</tr>
<tr>
<th>2</th>
<td>1</td>
<td>4.613897</td>
</tr>
</tbody>
</table>
</div>
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
}).groupby(['id', 'key']).max().reset_index().set_index('id').pipe(
lambda df: df[df.value > -2]
).pipe(
lambda df: df.add_suffix('_l').join(df.add_suffix('_r')) # suffixを追加して自分と結合
)
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>key_l</th>
<th>value_l</th>
<th>key_r</th>
<th>value_r</th>
</tr>
<tr>
<th>id</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>-2</th>
<td>1</td>
<td>-0.405903</td>
<td>1</td>
<td>-0.405903</td>
</tr>
<tr>
<th>-1</th>
<td>0</td>
<td>2.323369</td>
<td>0</td>
<td>2.323369</td>
</tr>
<tr>
<th>-0</th>
<td>0</td>
<td>1.296050</td>
<td>0</td>
<td>1.296050</td>
</tr>
<tr>
<th>-0</th>
<td>0</td>
<td>1.296050</td>
<td>1</td>
<td>3.376725</td>
</tr>
<tr>
<th>-0</th>
<td>0</td>
<td>1.296050</td>
<td>2</td>
<td>2.064360</td>
</tr>
<tr>
<th>-0</th>
<td>1</td>
<td>3.376725</td>
<td>0</td>
<td>1.296050</td>
</tr>
<tr>
<th>-0</th>
<td>1</td>
<td>3.376725</td>
<td>1</td>
<td>3.376725</td>
</tr>
<tr>
<th>-0</th>
<td>1</td>
<td>3.376725</td>
<td>2</td>
<td>2.064360</td>
</tr>
<tr>
<th>-0</th>
<td>2</td>
<td>2.064360</td>
<td>0</td>
<td>1.296050</td>
</tr>
<tr>
<th>-0</th>
<td>2</td>
<td>2.064360</td>
<td>1</td>
<td>3.376725</td>
</tr>
<tr>
<th>-0</th>
<td>2</td>
<td>2.064360</td>
<td>2</td>
<td>2.064360</td>
</tr>
<tr>
<th>1</th>
<td>0</td>
<td>2.915812</td>
<td>0</td>
<td>2.915812</td>
</tr>
<tr>
<th>1</th>
<td>0</td>
<td>2.915812</td>
<td>1</td>
<td>1.237605</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>1.237605</td>
<td>0</td>
<td>2.915812</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>1.237605</td>
<td>1</td>
<td>1.237605</td>
</tr>
<tr>
<th>2</th>
<td>0</td>
<td>0.323913</td>
<td>0</td>
<td>0.323913</td>
</tr>
<tr>
<th>2</th>
<td>0</td>
<td>0.323913</td>
<td>1</td>
<td>4.613897</td>
</tr>
<tr>
<th>2</th>
<td>1</td>
<td>4.613897</td>
<td>0</td>
<td>0.323913</td>
</tr>
<tr>
<th>2</th>
<td>1</td>
<td>4.613897</td>
<td>1</td>
<td>4.613897</td>
</tr>
</tbody>
</table>
</div>
# 一旦いろいろ整理
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
}).groupby(['id', 'key']).max().reset_index().set_index('id').pipe(
lambda df: df[df.value > -2]
).pipe(
lambda df: df.add_suffix('_l').join(df.add_suffix('_r'))
).reset_index().groupby(['id', 'key_l']).sum().reset_index().assign(
key=lambda df: df.value_l.map(lambda x: x*10).round().map(lambda x: 'label' + str(int(x)))
).rename(columns={
'value_r': 'value'
})[['key', 'value']]
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>key</th>
<th>value</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>label-4</td>
<td>-0.405903</td>
</tr>
<tr>
<th>1</th>
<td>label23</td>
<td>2.323369</td>
</tr>
<tr>
<th>2</th>
<td>label39</td>
<td>6.737135</td>
</tr>
<tr>
<th>3</th>
<td>label101</td>
<td>6.737135</td>
</tr>
<tr>
<th>4</th>
<td>label62</td>
<td>6.737135</td>
</tr>
<tr>
<th>5</th>
<td>label58</td>
<td>4.153417</td>
</tr>
<tr>
<th>6</th>
<td>label25</td>
<td>4.153417</td>
</tr>
<tr>
<th>7</th>
<td>label6</td>
<td>4.937809</td>
</tr>
<tr>
<th>8</th>
<td>label92</td>
<td>4.937809</td>
</tr>
</tbody>
</table>
</div>
# pivot
いわゆる'縦持ち'から'横持ち'への変換です.
ちなみに無名のindexに対して`reset_index()`を使うと新たに`index`という列が出来る
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
}).groupby(['id', 'key']).max().reset_index().set_index('id').pipe(
lambda df: df[df.value > -2]
).pipe(
lambda df: df.add_suffix('_l').join(df.add_suffix('_r'))
).reset_index().groupby(['id', 'key_l']).sum().reset_index().assign(
key=lambda df: df.value_l.map(lambda x: x*10).round().map(lambda x: 'label' + str(int(x)))
).rename(columns={
'value_r': 'value'
})[['key', 'value']].reset_index()
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>index</th>
<th>key</th>
<th>value</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>label-4</td>
<td>-0.405903</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>label23</td>
<td>2.323369</td>
</tr>
<tr>
<th>2</th>
<td>2</td>
<td>label39</td>
<td>6.737135</td>
</tr>
<tr>
<th>3</th>
<td>3</td>
<td>label101</td>
<td>6.737135</td>
</tr>
<tr>
<th>4</th>
<td>4</td>
<td>label62</td>
<td>6.737135</td>
</tr>
<tr>
<th>5</th>
<td>5</td>
<td>label58</td>
<td>4.153417</td>
</tr>
<tr>
<th>6</th>
<td>6</td>
<td>label25</td>
<td>4.153417</td>
</tr>
<tr>
<th>7</th>
<td>7</td>
<td>label6</td>
<td>4.937809</td>
</tr>
<tr>
<th>8</th>
<td>8</td>
<td>label92</td>
<td>4.937809</td>
</tr>
</tbody>
</table>
</div>
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
}).groupby(['id', 'key']).max().reset_index().set_index('id').pipe(
lambda df: df[df.value > -2]
).pipe(
lambda df: df.add_suffix('_l').join(df.add_suffix('_r'))
).reset_index().groupby(['id', 'key_l']).sum().reset_index().assign(
key=lambda df: df.value_l.map(lambda x: x*10).round().map(lambda x: 'label' + str(int(x)))
).rename(columns={
'value_r': 'value'
})[['key', 'value']].reset_index().pivot(
'index',
'key',
'value'
)
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>key</th>
<th>label-4</th>
<th>label101</th>
<th>label23</th>
<th>label25</th>
<th>label39</th>
<th>label58</th>
<th>label6</th>
<th>label62</th>
<th>label92</th>
</tr>
<tr>
<th>index</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>-0.405903</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
</tr>
<tr>
<th>1</th>
<td>NaN</td>
<td>NaN</td>
<td>2.323369</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
</tr>
<tr>
<th>2</th>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>6.737135</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
</tr>
<tr>
<th>3</th>
<td>NaN</td>
<td>6.737135</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
</tr>
<tr>
<th>4</th>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>6.737135</td>
<td>NaN</td>
</tr>
<tr>
<th>5</th>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>4.153417</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
</tr>
<tr>
<th>6</th>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>4.153417</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
</tr>
<tr>
<th>7</th>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>4.937809</td>
<td>NaN</td>
<td>NaN</td>
</tr>
<tr>
<th>8</th>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>NaN</td>
<td>4.937809</td>
</tr>
</tbody>
</table>
</div>
# fillna
いわゆるnull値を埋める.
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
}).groupby(['id', 'key']).max().reset_index().set_index('id').pipe(
lambda df: df[df.value > -2]
).pipe(
lambda df: df.add_suffix('_l').join(df.add_suffix('_r'))
).reset_index().groupby(['id', 'key_l']).sum().reset_index().assign(
key=lambda df: df.value_l.map(lambda x: x*10).round().map(lambda x: 'label' + str(int(x)))
).rename(columns={
'value_r': 'value'
})[['key', 'value']].reset_index().pivot(
'index',
'key',
'value'
).fillna(0)
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>key</th>
<th>label-4</th>
<th>label101</th>
<th>label23</th>
<th>label25</th>
<th>label39</th>
<th>label58</th>
<th>label6</th>
<th>label62</th>
<th>label92</th>
</tr>
<tr>
<th>index</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>-0.405903</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
</tr>
<tr>
<th>1</th>
<td>0.000000</td>
<td>0.000000</td>
<td>2.323369</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
</tr>
<tr>
<th>2</th>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>6.737135</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
</tr>
<tr>
<th>3</th>
<td>0.000000</td>
<td>6.737135</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
</tr>
<tr>
<th>4</th>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>6.737135</td>
<td>0.000000</td>
</tr>
<tr>
<th>5</th>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>4.153417</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
</tr>
<tr>
<th>6</th>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>4.153417</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
</tr>
<tr>
<th>7</th>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>4.937809</td>
<td>0.000000</td>
<td>0.000000</td>
</tr>
<tr>
<th>8</th>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>0.000000</td>
<td>4.937809</td>
</tr>
</tbody>
</table>
</div>
# astype
データ型のキャスト.csvに書きだしたとき `0.0000000....`ってならないようになります.
本当はもうちょっと複雑ですがきほんはこれです.
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
}).groupby(['id', 'key']).max().reset_index().set_index('id').pipe(
lambda df: df[df.value > -2]
).pipe(
lambda df: df.add_suffix('_l').join(df.add_suffix('_r'))
).reset_index().groupby(['id', 'key_l']).sum().reset_index().assign(
key=lambda df: df.value_l.map(lambda x: x*10).round().map(lambda x: 'label' + str(int(x)))
).rename(columns={
'value_r': 'value'
})[['key', 'value']].reset_index().pivot(
'index',
'key',
'value'
).fillna(0).astype(np.int8)
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>key</th>
<th>label-4</th>
<th>label101</th>
<th>label23</th>
<th>label25</th>
<th>label39</th>
<th>label58</th>
<th>label6</th>
<th>label62</th>
<th>label92</th>
</tr>
<tr>
<th>index</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>1</th>
<td>0</td>
<td>0</td>
<td>2</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>2</th>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>6</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>3</th>
<td>0</td>
<td>6</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>4</th>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>6</td>
<td>0</td>
</tr>
<tr>
<th>5</th>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>4</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>6</th>
<td>0</td>
<td>0</td>
<td>0</td>
<td>4</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>7</th>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>4</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>8</th>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>4</td>
</tr>
</tbody>
</table>
</div>
# stack
いわゆる横持ちから縦持ちへの変換.
階層インデックス持ちのSeriesが帰ります.データフレームの世界で輝き続けるにはpipeと一緒に使います.
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
}).groupby(['id', 'key']).max().reset_index().set_index('id').pipe(
lambda df: df[df.value > -2]
).pipe(
lambda df: df.add_suffix('_l').join(df.add_suffix('_r'))
).reset_index().groupby(['id', 'key_l']).sum().reset_index().assign(
key=lambda df: df.value_l.map(lambda x: x*10).round().map(lambda x: 'label' + str(int(x)))
).rename(columns={
'value_r': 'value'
})[['key', 'value']].reset_index().pivot(
'index',
'key',
'value'
).fillna(0).astype(np.int8).stack()
```
index key
0 label-4 0
label101 0
label23 0
label25 0
label39 0
label58 0
label6 0
label62 0
label92 0
1 label-4 0
label101 0
label23 2
label25 0
label39 0
label58 0
label6 0
label62 0
label92 0
2 label-4 0
label101 0
label23 0
label25 0
label39 6
label58 0
label6 0
label62 0
label92 0
3 label-4 0
label101 6
label23 0
..
5 label6 0
label62 0
label92 0
6 label-4 0
label101 0
label23 0
label25 4
label39 0
label58 0
label6 0
label62 0
label92 0
7 label-4 0
label101 0
label23 0
label25 0
label39 0
label58 0
label6 4
label62 0
label92 0
8 label-4 0
label101 0
label23 0
label25 0
label39 0
label58 0
label6 0
label62 0
label92 4
dtype: int8
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
}).groupby(['id', 'key']).max().reset_index().set_index('id').pipe(
lambda df: df[df.value > -2]
).pipe(
lambda df: df.add_suffix('_l').join(df.add_suffix('_r'))
).reset_index().groupby(['id', 'key_l']).sum().reset_index().assign(
key=lambda df: df.value_l.map(lambda x: x*10).round().map(lambda x: 'label' + str(int(x)))
).rename(columns={
'value_r': 'value'
})[['key', 'value']].reset_index().pivot(
'index',
'key',
'value'
).fillna(0).astype(np.int8).pipe(
lambda df: pd.DataFrame({'value':df.stack()})
)
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th></th>
<th>value</th>
</tr>
<tr>
<th>index</th>
<th>key</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="9" valign="top">0</th>
<th>label-4</th>
<td>0</td>
</tr>
<tr>
<th>label101</th>
<td>0</td>
</tr>
<tr>
<th>label23</th>
<td>0</td>
</tr>
<tr>
<th>label25</th>
<td>0</td>
</tr>
<tr>
<th>label39</th>
<td>0</td>
</tr>
<tr>
<th>label58</th>
<td>0</td>
</tr>
<tr>
<th>label6</th>
<td>0</td>
</tr>
<tr>
<th>label62</th>
<td>0</td>
</tr>
<tr>
<th>label92</th>
<td>0</td>
</tr>
<tr>
<th rowspan="9" valign="top">1</th>
<th>label-4</th>
<td>0</td>
</tr>
<tr>
<th>label101</th>
<td>0</td>
</tr>
<tr>
<th>label23</th>
<td>2</td>
</tr>
<tr>
<th>label25</th>
<td>0</td>
</tr>
<tr>
<th>label39</th>
<td>0</td>
</tr>
<tr>
<th>label58</th>
<td>0</td>
</tr>
<tr>
<th>label6</th>
<td>0</td>
</tr>
<tr>
<th>label62</th>
<td>0</td>
</tr>
<tr>
<th>label92</th>
<td>0</td>
</tr>
<tr>
<th rowspan="9" valign="top">2</th>
<th>label-4</th>
<td>0</td>
</tr>
<tr>
<th>label101</th>
<td>0</td>
</tr>
<tr>
<th>label23</th>
<td>0</td>
</tr>
<tr>
<th>label25</th>
<td>0</td>
</tr>
<tr>
<th>label39</th>
<td>6</td>
</tr>
<tr>
<th>label58</th>
<td>0</td>
</tr>
<tr>
<th>label6</th>
<td>0</td>
</tr>
<tr>
<th>label62</th>
<td>0</td>
</tr>
<tr>
<th>label92</th>
<td>0</td>
</tr>
<tr>
<th rowspan="3" valign="top">3</th>
<th>label-4</th>
<td>0</td>
</tr>
<tr>
<th>label101</th>
<td>6</td>
</tr>
<tr>
<th>label23</th>
<td>0</td>
</tr>
<tr>
<th>...</th>
<th>...</th>
<td>...</td>
</tr>
<tr>
<th rowspan="3" valign="top">5</th>
<th>label6</th>
<td>0</td>
</tr>
<tr>
<th>label62</th>
<td>0</td>
</tr>
<tr>
<th>label92</th>
<td>0</td>
</tr>
<tr>
<th rowspan="9" valign="top">6</th>
<th>label-4</th>
<td>0</td>
</tr>
<tr>
<th>label101</th>
<td>0</td>
</tr>
<tr>
<th>label23</th>
<td>0</td>
</tr>
<tr>
<th>label25</th>
<td>4</td>
</tr>
<tr>
<th>label39</th>
<td>0</td>
</tr>
<tr>
<th>label58</th>
<td>0</td>
</tr>
<tr>
<th>label6</th>
<td>0</td>
</tr>
<tr>
<th>label62</th>
<td>0</td>
</tr>
<tr>
<th>label92</th>
<td>0</td>
</tr>
<tr>
<th rowspan="9" valign="top">7</th>
<th>label-4</th>
<td>0</td>
</tr>
<tr>
<th>label101</th>
<td>0</td>
</tr>
<tr>
<th>label23</th>
<td>0</td>
</tr>
<tr>
<th>label25</th>
<td>0</td>
</tr>
<tr>
<th>label39</th>
<td>0</td>
</tr>
<tr>
<th>label58</th>
<td>0</td>
</tr>
<tr>
<th>label6</th>
<td>4</td>
</tr>
<tr>
<th>label62</th>
<td>0</td>
</tr>
<tr>
<th>label92</th>
<td>0</td>
</tr>
<tr>
<th rowspan="9" valign="top">8</th>
<th>label-4</th>
<td>0</td>
</tr>
<tr>
<th>label101</th>
<td>0</td>
</tr>
<tr>
<th>label23</th>
<td>0</td>
</tr>
<tr>
<th>label25</th>
<td>0</td>
</tr>
<tr>
<th>label39</th>
<td>0</td>
</tr>
<tr>
<th>label58</th>
<td>0</td>
</tr>
<tr>
<th>label6</th>
<td>0</td>
</tr>
<tr>
<th>label62</th>
<td>0</td>
</tr>
<tr>
<th>label92</th>
<td>4</td>
</tr>
</tbody>
</table>
<p>81 rows × 1 columns</p>
</div>
```python
df.assign(
round_A=lambda df: df.A.round(), # 四捨五入
round_B=lambda df: df.B.round(), # 四捨五入
total=lambda df: df.apply(lambda row: sum(row), axis=1) # A-Eの和
)[['round_A', 'round_B', 'total']].rename(columns={
'round_A': 'id',
'round_B': 'key',
'total': 'value'
}).groupby(['id', 'key']).max().reset_index().set_index('id').pipe(
lambda df: df[df.value > -2]
).pipe(
lambda df: df.add_suffix('_l').join(df.add_suffix('_r'))
).reset_index().groupby(['id', 'key_l']).sum().reset_index().assign(
key=lambda df: df.value_l.map(lambda x: x*10).round().map(lambda x: 'label' + str(int(x)))
).rename(columns={
'value_r': 'value'
})[['key', 'value']].reset_index().pivot(
'index',
'key',
'value'
).fillna(0).astype(np.int8).pipe(
lambda df: pd.DataFrame({'value':df.stack()})
).pipe(
lambda df: df[df.value != 0]
)
```
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th></th>
<th>value</th>
</tr>
<tr>
<th>index</th>
<th>key</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>1</th>
<th>label23</th>
<td>2</td>
</tr>
<tr>
<th>2</th>
<th>label39</th>
<td>6</td>
</tr>
<tr>
<th>3</th>
<th>label101</th>
<td>6</td>
</tr>
<tr>
<th>4</th>
<th>label62</th>
<td>6</td>
</tr>
<tr>
<th>5</th>
<th>label58</th>
<td>4</td>
</tr>
<tr>
<th>6</th>
<th>label25</th>
<td>4</td>
</tr>
<tr>
<th>7</th>
<th>label6</th>
<td>4</td>
</tr>
<tr>
<th>8</th>
<th>label92</th>
<td>4</td>
</tr>
</tbody>
</table>
</div>
# さいごに
ほらね?再帰代入なんてもうやめよみんな!!
```python
```