为了账号安全,请及时绑定邮箱和手机立即绑定

如果文本包含在另一个数据帧中,则使用二进制名称标记行

如果文本包含在另一个数据帧中,则使用二进制名称标记行

翻阅古今 2022-09-06 21:23:25
我正在研究采矿调查数据。我能够标记某些关键字的行:survey['Rude'] = survey['Comment Text'].str.contains('rude', na=False, regex=True).astype(int)现在,我想标记任何包含名称的行。我有另一个包含常见美国名称的数据帧。以下是我认为可以工作的内容,但它没有标记任何行,并且我已经验证了名称确实存在于“注释文本”中for row in survey:       for word in survey['Comment Text']:        survey['Name'] = 0        if word in names['Name']:            survey['Name'] = 1
查看完整描述

1 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

您没有正确循环遍历序列。 遍历 中的列名。 遍历注释字符串。 创建包含所有 .for row in survey:surveyfor word in survey['Comment Text']:survey['Name'] = 00s

您可以使用 set intersections 和 apply()来避免所有通过行的循环:

    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

作为奖励,返回以获取每行的匹配次数。len(s1.intersection(s2))


查看完整回答
反对 回复 2022-09-06
  • 1 回答
  • 0 关注
  • 61 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信