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

JavaScript

I expect the output to look like this:

JavaScript

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