python正则匹配统计_Python pandas计算字符串中正则表达式匹配的数量

我有一个包含句子的数据框和一个按主题分组的术语词典,我想在其中计算每个主题匹配的术语数。在import pandas as pd

terms = {'animals':["fox","deer","eagle"],

'people':['John', 'Rob','Steve'],

'games':['basketball', 'football', 'hockey']

}

df=pd.DataFrame({

'Score': [4,6,2,7,8],

'Foo': ['The quick brown fox was playing basketball today','John and Rob visited the eagles nest, the foxes ran away','Bill smells like a wet dog','Steve threw the football at a deer. But the football missed','Sheriff John does not like hockey']

})

到目前为止,我已经为主题创建了列,并通过遍历字典将其标记为1。在

^{pr2}$

我得到:>>>

Foo Score animals games \

0 The quick brown fox was playing basketball today 4 1 1

1 John and Rob visited the eagles nest, the foxe... 6 1 NaN

2 Bill smells like a wet dog 2 NaN NaN

3 Steve threw the football at a deer. But the fo... 7 1 1

4 Sheriff John does not like hockey 8 NaN 1

people

0 NaN

1 1

2 NaN

3 1

4 1

计算句子中出现的每个主题的字数的最佳方法是什么?还有没有一种更有效的方法可以不使用cython来遍历字典?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值