pd.get_dummies(df)、pd.get_dummies (df ['열 이름'])

7330 단어 python
import numpy as np

import pandas as pd

df = pd.DataFrame({'id':[1,2,3,4,5,6,3,2],'raw_grade':['a','b','c','d','a','b','c','d']})

df
Out[13]: 
   id raw_grade
0   1         a
1   2         b
2   3         c
3   4         d
4   5         a
5   6         b
6   3         c
7   2         d

pd.get_dummies(df)

Out[14]: 
   id  raw_grade_a  raw_grade_b  raw_grade_c  raw_grade_d
0   1            1            0            0            0
1   2            0            1            0            0
2   3            0            0            1            0
3   4            0            0            0            1
4   5            1            0            0            0
5   6            0            1            0            0
6   3            0            0            1            0
7   2            0            0            0            1

pd.get_dummies(df['raw_grade'])
Out[15]: 
   a  b  c  d
0  1  0  0  0
1  0  1  0  0
2  0  0  1  0
3  0  0  0  1
4  1  0  0  0
5  0  1  0  0
6  0  0  1  0
7  0  0  0  1

좋은 웹페이지 즐겨찾기