从另一列

时间:2016-02-29 23:09:08

标签: python parsing pandas split dataframe

我的数据框看起来像:

Groupe       Id   MotherName   FatherName    Field
Advanced    56    Laure         James        English-107,Economics, Management, History, Philosophy
Middle      11    Ann           Nicolas      Web-development, Java-2
Advanced    6     Helen         Franc        Literature, English-2
Beginner    43    Laure         James        Mathematics, History, Philosophy, Literature
Middle      14    Naomi         Franc        Java-2, Management, English-107

为了进一步处理数据,我需要拆分Field列,并将其替换为多个列,如下所示:

Id English-107 Economics Management History Web-development Java-2 Literature English-2 Mathematics Philosophy
56     1         1          1           1           0          0       0             0          0         1
11     0         0          0           0           1           1      0             0            0          0

因此,这些列可以附加到初始数据帧。我不知道如何制作它,因为只是基本的分裂,如

pd.DataFrame(df.Field.str.split(',',1).tolist())

无法解决我的问题,因为我需要的列不仅基于列表中的位置,还基于列表中的每个唯一值。你知道我怎么能接近吗?

1 个答案:

答案 0 :(得分:2)

您可以使用concatstr.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  

如果您需要计数值,可以使用pivot_table(我添加一个字符串Economics进行测试):

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