Creating new binary columns from single string column in pandas
You can do this easily with pd.get_dummies
(docs):
In [37]: df = pd.DataFrame(['Slow', 'Normal', 'Fast', 'Slow'], columns=['Speed'])
In [38]: df
Out[38]:
Speed
0 Slow
1 Normal
2 Fast
3 Slow
In [39]: pd.get_dummies(df['Speed'])
Out[39]:
Fast Normal Slow
0 0 0 1
1 0 1 0
2 1 0 0
3 0 0 1
Here is one solution:
df['Normal'] = df.Speed.apply(lambda x: 1 if x == "Normal" else 0)
df['Slow'] = df.Speed.apply(lambda x: 1 if x == "Slow" else 0)
df['Fast'] = df.Speed.apply(lambda x: 1 if x == "Fast" else 0)