1 回答

TA貢獻1856條經(jīng)驗 獲得超17個贊
您沒有正確循環(huán)遍歷序列。 遍歷 中的列名。 遍歷注釋字符串。 創(chuàng)建包含所有 .for row in survey:
survey
for word in survey['Comment Text']:
survey['Name'] = 0
0s
您可以使用 set intersections 和 apply()來避免所有通過行的循環(huán):
survey = pd.DataFrame({'Comment_Text':['Hi rcriii',
'Hi yourself stranger',
'say hi to Justin for me']})
names = pd.DataFrame({'Name':['rcriii', 'Justin', 'Susan', 'murgatroyd']})
s2 = set(names['Name'])
def is_there_a_name(s):
s1 = set(s.split())
if len(s1.intersection(s2))>0:
return 1
else:
return 0
survey['Name'] = survey['Comment_Text'].apply(is_there_a_name)
print(names)
print(survey)
Name
0 rcriii
1 Justin
2 Susan
3 murgatroyd
Comment_Text Name
0 Hi rcriii 1
1 Hi yourself stranger 0
2 say hi to Justin for me 1
作為獎勵,返回以獲取每行的匹配次數(shù)。len(s1.intersection(s2))
添加回答
舉報