65944
以下に示したようなデータリストがあります。  
列見出しは `data1,data2,data3`、行見出しは `A,B,C,...M` として、以下のようなダミー変数を作りたいです。

Pythonを使ってどのようにすれば良いでしょうか？  
よろしくお願いします。

* * *

**データリスト**

```

    data1＝["A","B","C","D","E"]
    data2＝["A","B","G","H","I"]
    data3＝["A","J","K","L","M"]
    
```

**ダミー変数**

[![画像の説明をここに入力](https://i.stack.imgur.com/P4bph.png)](https://i.stack.imgur.com/P4bph.png)
CC BY-SA 4.0
2020-04-25T01:29:38.023
65935
2020-04-25T05:34:26.230
2020-04-25T05:34:26.230
3060
21301
question
python
pythonで質的変数→量的変数（ダミー変数化）
pandas を使って DataFrameを作成し、変換することで

```

    import pandas as pd
    
    data1 = ["A","B","C","D","E"]
    data2 = ["A","B","G","H","I"]
    data3 = ["A","J","K","L","M"]
    
    df = pd.DataFrame([data1,data2,data3], index=['data1','data2','data3'])
    df = df.apply(lambda row: row.value_counts(),axis=1).fillna(0).astype(int)
    print(df)
    #       A  B  C  D  E  G  H  I  J  K  L  M
    #data1  1  1  1  1  1  0  0  0  0  0  0  0
    #data2  1  1  0  0  0  1  1  1  0  0  0  0
    #data3  1  0  0  0  0  0  0  0  1  1  1  1
    
```

のように実現できます。

ただし、上記の方法は data1,data2,data3 が同サイズの時しか使えません。

data1,data2,data3 のサイズが違う場合はそれぞれのデータでDataFrameを作成して結合すると実現できます。

```

    import pandas as pd
    
    data1 = ["A","B","C","D","E"]
    data2 = ["A","B","G","H","I"]
    data3 = ["A","J","K","L","M"]
    
    datas = [pd.DataFrame(1, columns=d, index=[n]) for d,n in zip([data1,data2,data3], ['data1','data2','data3'])]
    df = pd.concat(datas).fillna(0).astype(int)
    print(df)
    #       A  B  C  D  E  G  H  I  J  K  L  M
    #data1  1  1  1  1  1  0  0  0  0  0  0  0
    #data2  1  1  0  0  0  1  1  1  0  0  0  0
    #data3  1  0  0  0  0  0  0  0  1  1  1  1
    
```

Series使っても書けますが、コード量的にはあまり変わりませんね。

```

    datas = [pd.Series(1, index=d, name=n) for d,n in zip([data1,data2,data3], ['data1','data2','data3'])]
    df = pd.concat(datas, axis=1).fillna(0).astype(int).T
    print(df)
    #       A  B  C  D  E  G  H  I  J  K  L  M
    #data1  1  1  1  1  1  0  0  0  0  0  0  0
    #data2  1  1  0  0  0  1  1  1  0  0  0  0
    #data3  1  0  0  0  0  0  0  0  1  1  1  1
    
```
CC BY-SA 4.0
2020-04-25T03:00:44.587
65941
2020-04-25T03:00:44.587
24801
65935
answer
カラムは `string.ascii_uppercase` から作成しています。この場合、`data1`, `data2`, `data3`
には含まれていない `F` も結果に含める事ができます。

```

    from string import ascii_uppercase
    from collections import Counter
    import pandas as pd
    
    data1 = ["A", "B", "C", "D", "E"]
    data2 = ["A", "B", "G", "H", "I"]
    data3 = ["A", "J", "K", "L", "M"]
    
    pd.DataFrame(
      [Counter(x) for x in (data1, data2, data3)],
      index=['data1', 'data2', 'data3'],
      columns=list(ascii_uppercase)[:13],
      dtype='Int64'
    ).fillna(0)
    
    =>
           A  B  C  D  E  F  G  H  I  J  K  L  M
    data1  1  1  1  1  1  0  0  0  0  0  0  0  0
    data2  1  1  0  0  0  0  1  1  1  0  0  0  0
    data3  1  0  0  0  0  0  0  0  0  1  1  1  1
    
```
CC BY-SA 4.0
2020-04-25T03:50:51.183
65944
2020-04-25T03:50:51.183
65935
answer
既に解決済みですが、別解です。ライブラリは使っていません。

**コード**

```

    data1 = ["A","B","C","D","E"]
    data2 = ["A","B","G","H","I"]
    data3 = ["A","J","K","L","M"]
    row_heading = list(map(lambda x: chr(x),list(range(ord("A"),ord("M")+1))))
    out_1 = [(1 if x in data1 else 0) for x in row_heading]
    out_2 = [(1 if x in data2 else 0) for x in row_heading]
    out_3 = [(1 if x in data3 else 0) for x in row_heading]
    
    print(row_heading)
    print(data1)
    print(out_1)
    print(data2)
    print(out_2)
    print(data3)
    print(out_3)
    
```

**結果**

```

    ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M']
    ['A', 'B', 'C', 'D', 'E']
    [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
    ['A', 'B', 'G', 'H', 'I']
    [1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0]
    ['A', 'J', 'K', 'L', 'M']
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1]
    
```
CC BY-SA 4.0
2020-04-25T04:15:42.697
65945
2020-04-25T04:15:42.697
35558
65935
answer