Issue
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
Solution
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
Answered By - Mayank Porwal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.