[英]How can I check a column for a sub-string and update another column if found?
[英]How can I check if a string in a column is a sub-string in another column using dataframe and pandas
我正在使用假新闻检测器,我想检查新闻标题[TITLE]的内容是否在新闻[TEXT]的内容之内。 如果结果为True
,则应返回1,如果结果为False
,则应返回0。返回值形成一个新列
这项工作是供研究出版物使用的。 我尝试为此使用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'})
我希望输出看起来像这样:
News_column
1
1
0
0
0
1
您可以像这样在数据框的每一行上使用Apply:
news1['News_column'] = news1.apply(lambda x: 1 if x['TITLE'] in x['TEXT'] else 0, axis=1)
应该返回预期的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.