butter
butter

Reputation: 63

add a binary column based on condition

I have a dataframe "veh_contract2_df" like this :

FUEL_CODE   FUEL_TYPE 
1           MARGE+PLUS
10          DIESEL

I would like to add a column "hybrid" which should contain "Y" if there is "+" in FUEL_TYPE and "N" if not.

sub_str = "+"
if(veh_contract2_df.loc[(veh_contract2_df['FUEL_TYPE'].find(sub_str)==-1)]):
    veh_contract2_df['HYBRIDE'] = "Y"
    else:
    veh_contract2_df['HYBRIDE'] ="N"

But i got this error : SyntaxError: invalid syntax (at line 71)

Any idea please? thanks

Upvotes: 1

Views: 975

Answers (1)

Mayank Porwal
Mayank Porwal

Reputation: 34046

Use numpy.where:

In [1923]: import numpy as np

In [1924]: df['hybrid'] = np.where(df.FUEL_TYPE.str.contains('+', regex=False), 'Y', 'N')

In [1925]: df
Out[1925]: 
   FUEL_CODE   FUEL_TYPE hybrid
0          1  MARGE+PLUS      Y
1         10      DIESEL      N

Upvotes: 6

Related Questions