Creating new binary columns from a single row column in pandas

I have seen this before and just can't remember this function.

Let's say I have a “Speed” column, and each row has 1 of these values:

'Slow', 'Normal', 'Fast' 

How to create a new data framework with all my rows except the "Speed" column, which now makes 3 columns: "Slow" "Normal" and "Fast", which has all my rows with a label of 1 in any column, the old column "Speed" was. Therefore, if I had:

 print df['Speed'].ix[0] > 'Normal' 

I would not expect this:

 print df['Normal'].ix[0] >1 print df['Slow'].ix[0] >0 
+7
python pandas
source share
3 answers

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 
+11
source share

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) 
+4
source share

This has another method:

 df = pd.DataFrame(['Slow','Fast','Normal','Normal'],columns=['Speed']) df['Normal'] = np.where(df['Speed'] == 'Normal', 1 ,0) df['Fast'] = np.where(df['Speed'] == 'Fast', 1 ,0) df['Slow'] = np.where(df['Speed'] == 'Slow', 1 ,0) df Speed Normal Fast Slow 0 Slow 0 0 1 1 Fast 0 1 0 2 Normal 1 0 0 3 Normal 1 0 1 

0
source share

All Articles