Reputation: 426
How can I add/create a column which returns a 1, if the value in the column contains < or << and 0 if it does not, based on a column df['value']?
df['INDICATOR'] = [1 if x.str.contains("<|<<") else 0 for x in df['value']]
My attempt above returns an attributeError: 'str object has no attrbute 'str'
Upvotes: 0
Views: 39
Reputation: 863196
Create mask and then Series.astype
, Series.view
or numpy.where
for convert True/False
to 1/0
:
m = df['value'].str.contains("<|<<")
#simplify mask - if contains << then contain <
m = df['value'].str.contains("<")
df['INDICATOR'] = m.astype(int)
df['INDICATOR'] = m.view('i1')
df['INDICATOR'] = np.where(m, 1, 0)
Upvotes: 2