I have the dataframe that looks like:
Groupe Id MotherName FatherName Field
Advanced 56 Laure James English-107,Economics,
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