Create dynamic columns in dataframe using pandas

前端 未结 2 2069
-上瘾入骨i
-上瘾入骨i 2021-01-24 07:27

How to create dynamic columns from this pandas dataframe.

 Name, Sex
    a, M
    b, F
    c, M
    d, F

Expected dataframe:

Na         


        
2条回答
  •  情歌与酒
    2021-01-24 07:53

    Use get dummies:

    pd.concat([df['Name'], df['Sex'].str.get_dummies()], axis=1)
    Out: 
        Name   F   M
    0      a   0   1
    1      b   1   0
    2      c   0   1
    3      d   1   0
    

    df['Sex'].str.get_dummies() generates the dummies:

    df['Sex'].str.get_dummies()
    Out: 
        F   M
    0   0   1
    1   1   0
    2   0   1
    3   1   0
    

    and then you can use pd.concat to combine the result with the name column.

提交回复
热议问题