Converting a Pandas Dataframe column into one hot labels

前端 未结 4 1770
余生分开走
余生分开走 2020-12-20 16:08

I have a pandas dataframe similar to this:

  Col1   ABC
0  XYZ    A
1  XYZ    B
2  XYZ    C

By using the pandas get_dummies()

4条回答
  •  伪装坚强ぢ
    2020-12-20 16:40

    You can just use tolist():

    df['ABC'] = pd.get_dummies(df.ABC).values.tolist()
    
      Col1        ABC
    0  XYZ  [1, 0, 0]
    1  XYZ  [0, 1, 0]
    2  XYZ  [0, 0, 1]
    

提交回复
热议问题