create multiple columns from list of values of another column

后端 未结 1 1112
野性不改
野性不改 2020-12-21 10:38

I have the dataframe that looks like:

Groupe       Id   MotherName   FatherName    Field
Advanced    56    Laure         James        English-107,Economics,          


        
相关标签:
1条回答
  • 2020-12-21 11:32

    You can use concat and str.get_dummies:

    print pd.concat([df['Id'], df['Field'].str.get_dummies(sep=",")], axis=1)
       Id  Economics  English-107  English-2  History  Java-2  Literature  \
    0  56          1            1          0        1       0           0   
    1  11          0            0          0        0       1           0   
    2   6          0            0          1        0       0           1   
    3  43          0            0          0        1       0           1   
    4  14          0            1          0        0       1           0   
    
       Management  Mathematics  Philosophy  Web-development  
    0           1            0           1                0  
    1           0            0           0                1  
    2           0            0           0                0  
    3           0            1           1                0  
    4           1            0           0                0  
    

    If you need count values, you can use pivot_table (I add one string Economics for testing):

    df1 = df['Field'].str.split(',',expand=True).stack()
                                                .groupby(level=0)
                                                .value_counts()
                                                .reset_index()
    df1.columns=['a','b','c']
    print df1.pivot_table(index='a',columns='b',values='c').fillna(0)
    b  Economics  English-107  English-2  History  Java-2  Literature  Management  \
    a                                                                               
    0          2            1          0        1       0           0           1   
    1          0            0          0        0       1           0           0   
    2          0            0          1        0       0           1           0   
    3          0            0          0        1       0           1           0   
    4          0            1          0        0       1           0           1   
    
    b  Mathematics  Philosophy  Web-development  
    a                                            
    0            0           1                0  
    1            0           0                1  
    2            0           0                0  
    3            1           1                0  
    4            0           0                0  
    
    0 讨论(0)
提交回复
热议问题