I would like to classify elements in rows (I am using pandas data frame) based on a list of strings:
list_topics=['orange', 'sports', 'technology', 'apple pie','fruits']
I want to see if websites contains one of that string in order to classify them.
For example:
Website
www.apple.com
www.orange_is_the_new_black.co.uk
...
www.mitapple.com
These elements are stored in a row (row[0]). I have tried as follows:
writer = csv.writer(f_output, lineterminator='\n')
reader = csv.reader(f_input)
header = next(reader)
header.append('Classification')
writer.writerow(header)
for row in reader:
check_el = ['not classified']
for x in list_topics:
if row[0].str.contains[x]:
check_el[0] = x
writer.writerow(row + match)
However it returns only not classified, rather than (expected output):
Website Topics
www.apple.com apple
www.orange_is_the_new_black.co.uk orange
... ...
www.mitapple.com apple
Could you please tell me how to compare each row to strings in the list and see if the row contains one of that string?
Thanks