簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM