Skip to content
Advertisement

How can I check if a string in a column is a sub-string in another column using dataframe and pandas

I am working on a fake news detector, I want to check if the content of the news headline [TITLE] is inside the content of the news [TEXT]. If the result is True it should return 1 and if it’s False it should return 0. the return value forms a new column

This work is for a research publication. I have tried using SVM for this

import pandas as pd
news1= pd.read_csv('dataset/id_title_author_text_label.csv')
news1.head()
news1['News_column'] = news1[news1['TITLE'].str.contain in news1['TEXT']]
news1['News_column'] = news1['News_column'].map({True: 'Yes', False: 'No'})

I expect the output to look like this:

News_column
1
1
0
0
0
1

Advertisement

Answer

You can use an apply on each row of your dataframe like this :

news1['News_column'] = news1.apply(lambda x: 1 if x['TITLE'] in x['TEXT'] else 0, axis=1)

Should return the expected result.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement