pandasでのデータフレームの縦横(wide-long)変換
pandasのデータフレームのlong型、wide型の変換について。例えば次の様なwide型データフレームを考える。
import pandas as pd
dt = pd.DataFrame({"ID":np.arange(5), "res_3":[0, 0, 1, 0, 1], "res_6":[1, 0, 1, 0, 0], 
                    "res_9":[0, 0, 0, 0, 1], "allocation":[1, 1, 0, 0, 0]})
   ID  res_3  res_6  res_9  allocation
0   0      0      1      0           1
1   1      0      0      0           1
2   2      1      1      0           0
3   3      0      0      0           0
4   4      1      0      1           0これは、例えば介入後3か月、6か月、9か月のアンケート結果と、介入割当のデータフレームである。wide型のままだと、固定効果を見たい時に解析ができないので、long型にする。単純には次のように
dt.stack()
0  ID            0
   res_3         0
   res_6         1
   res_9         0
   allocation    1
1  ID            1
   res_3         0
   res_6         0
   res_9         0
   allocation    1
2  ID            2
   res_3         1
   res_6         1
   res_9         0
   allocation    0
3  ID            3
   res_3         0
   res_6         0
   res_9         0
   allocation    0
4  ID            4
   res_3         1
   res_6         0
   res_9         1
   allocation    0
dtype: int64とすればいいだが、これは欲しいものではない。そこでpandasの
meltmethodを用いる(melt)。これはwide→long形式のデータフレーム変換である。methodでもfunctionでもどちらでも良い。
dt.melt()
pd.melt(dt)
      variable  value
0           ID      0
1           ID      1
2           ID      2
3           ID      3
4           ID      4
5        res_3      0
6        res_3      0
7        res_3      1
8        res_3      0
9        res_3      1
10       res_6      1
11       res_6      0
12       res_6      1
13       res_6      0
14       res_6      0
15       res_9      0
16       res_9      0
17       res_9      0
18       res_9      0
19       res_9      1
20  allocation      1
21  allocation      1
22  allocation      0
23  allocation      0
24  allocation      0meltのparameterはid_vars, value_vars, id_name, value_nameを主に用いる。id_varsに設定した項目はそのままになる。例えば、
dt.melt(id_vars = "ID")
    ID    variable  value
0    0       res_3      0
1    1       res_3      0
2    2       res_3      1
3    3       res_3      0
4    4       res_3      1
5    0       res_6      1
6    1       res_6      0
7    2       res_6      1
8    3       res_6      0
9    4       res_6      0
10   0       res_9      0
11   1       res_9      0
12   2       res_9      0
13   3       res_9      0
14   4       res_9      1
15   0  allocation      1
16   1  allocation      1
17   2  allocation      0
18   3  allocation      0
19   4  allocation      0とすると上のようになる。id_varsにはリストで複数項目を指定することもできる。
dt.melt(id_vars = ["ID", "allocation"])
    ID  allocation variable  value
0    0           1    res_3      0
1    1           1    res_3      0
2    2           0    res_3      1
3    3           0    res_3      0
4    4           0    res_3      1
5    0           1    res_6      1
6    1           1    res_6      0
7    2           0    res_6      1
8    3           0    res_6      0
9    4           0    res_6      0
10   0           1    res_9      0
11   1           1    res_9      0
12   2           0    res_9      0
13   3           0    res_9      0
14   4           0    res_9      1
  
  
  
  
コメント