Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting flag column depending on whether column contains a given string

Can anyone see why this isn't working?

Its trying to do; if Column Name Contains the text 'Andy', then make a column called Andy and set that row = to 1

df.loc[df['Name'].str.contains(['Andy']),'Andy']=1
like image 652
fred.schwartz Avatar asked Dec 13 '18 09:12

fred.schwartz


2 Answers

You have to remove list, need only string:

df.loc[df['Name'].str.contains('Andy'),'Andy'] = 1

For multiple values chain by |:

df.loc[df['Name'].str.contains('Andy|Andrew'),'Andy'] = 1
like image 192
jezrael Avatar answered Nov 03 '22 20:11

jezrael


pd.Series.str.contains requires for its pat argument a "Character sequence or regular expression", not a list.

Just use Boolean assignment and convert to int. This will set unmatched rows to 0. For example:

# Name includes 'Andy'
df['Andy'] = df['Name'].str.contains('Andy').astype(int)

# Name includes 'Andy' or 'Andrew'
df['Andy'] = df['Name'].str.contains('Andy|Andrew').astype(int)
like image 6
jpp Avatar answered Nov 03 '22 19:11

jpp