Reputation: 489
I have a pandas dataframe and I want to search through strings in column A
, if there's a match I want to append 1
to a new column, if there is no match I want to append a 0
.
My df currently looks like:
Column A | Column B | Column C
company one | 314 | 0.9
company one toast | 190 | 0.3
www.companyone | 380 | 0.87
companyone home | 850 | 0.1
toaster supplies | 1100 | 0.5
toast rack | 200 | 0.7
...
I'm trying to write a function which will read through column A
, and if there's a match with either company one or companyone, then append 1 on the end of the row. If there is no match, then append 0. The output I'm looking for is:
Column A | Column B | Column C | Branded
company one | 314 | 0.9 | 1
company one toast | 190 | 0.3 | 1
www.companyone | 380 | 0.87 | 1
companyone home | 850 | 0.1 | 1
toaster supplies | 1100 | 0.5 | 0
toast rack | 200 | 0.7 | 0
...
I've tried this function:
def branded(table):
if 'company.*?one' in table[table['Column A']]:
table['Branded'] = 1
else:
table['Branded'] = 0
return table.head()
However I get a KeyError
. I'm not sure what I'm missing though.
Upvotes: 0
Views: 799
Reputation: 6649
The solution posted by zipa is better in my opinion. However, thought of sharing this which is a tweak version in case the strings to be looked for are entirely of different pattern. You can add the words to the list and then perform something similar:
import pandas as pd
df = pd.DataFrame({'column':['company one','companyone', 'company two']})
search = ['company one', 'companyone']
string_search = '|'.join(search)
df['flag'] = df['column'].str.contains(string_search)
df['flag'] = df['flag'].map({True: 1, False: 0})
Upvotes: 0
Reputation: 27869
You can do it like this:
df['Branded'] = df['Column A'].str.contains('company.*?one')*1
Upvotes: 1