简体   繁体   English

如何使用DataFrame和Pandas检查列中的字符串是否是另一列中的子字符串

[英]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]. 我正在使用假新闻检测器,我想检查新闻标题[TITLE]的内容是否在新闻[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 如果结果为True ,则应返回1,如果结果为False ,则应返回0。返回值形成一个新列

This work is for a research publication. 这项工作是供研究出版物使用的。 I have tried using SVM for this 我尝试为此使用SVM

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

You can use an apply on each row of your dataframe like this : 您可以像这样在数据框的每一行上使用Apply:

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

Should return the expected result. 应该返回预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查子字符串的列并在找到时更新另一列? - How can I check a column for a sub-string and update another column if found? 如何有条件地替换熊猫数据框列中的子字符串? - How to replace a sub-string conditionally in a pandas dataframe column? 如何在 Python 的 Pandas 中用我想要的东西替换特定列的子字符串? - How can I replace a sub-string of a specific column by something I want in Pandas of Python? 如何将 Dataframe 列中的字符串与另一个 Dataframe 中的子字符串进行比较并提取值 - How to Compare String in a Dataframe column with a sub-string in another Dataframe and extract the value 用另一个列值的子字符串替换 dataframe 的 null 值 - Replacing null values of a dataframe with a sub-string of another column value Pandas 从另一列的子字符串创建新列 - Pandas make new columns from sub-string of another column 1 列的 Pandas 子字符串作为新列的值 - Pandas Sub-String of 1 Column as Value of a New Column 从 Pandas DataFrame 的一列中提取 2 个特殊字符之间的子字符串 - Extract sub-string between 2 special characters from one column of Pandas DataFrame 数据框通过搜索子字符串来切片列内容 - Dataframe to slice column content by searching sub-string 检查字符串是否包含pandas dataframe中同一列的子字符串 - check if string contains sub string from the same column in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM