python pandas 数据查找,使用python中的pandas检索数据列上的匹配单词数

I have a df,

Name Description

Ram Ram is one of the good cricketer

Sri Sri is one of the member

Kumar Kumar is a keeper

and a list,

my_list=["one","good","ravi","ball"]

I am trying to get the rows which are having atleast one keyword from my_list.

I tried,

mask=df["Description"].str.contains("|".join(my_list),na=False)

I am getting the output_df,

Name Description

Ram Ram is one of ONe crickete

Sri Sri is one of the member

Ravi Ravi is a player, ravi is playing

Kumar there is a BALL

I also want to add the keywords present in the "Description" and its counts in a separate columns,

My desired output is,

Name Description pre-keys keys count

Ram Ram is one of ONe crickete one,good,ONe one,good 2

Sri Sri is one of the member one one 1

Ravi Ravi is a player, ravi is playing Ravi,ravi ravi 1

Kumar there is a BALL ball ball 1

解决方案extracted = df['Description'].str.findall('(' + '|'.join(my_list) + ')')

df['keys'] = extracted.str.join(',')

df['count'] = extracted.str.len()

print (df)

Name Description keys count

0 Ram Ram is one of the good cricketer one,good 2

1 Sri Sri is one of the member one 1

EDIT:

import re

my_list=["ONE","good"]

extracted = df['Description'].str.findall('(' + '|'.join(my_list) + ')', flags=re.IGNORECASE)

df['keys'] = extracted.str.join(',')

df['count'] = extracted.str.len()

print (df)

Name Description keys count

0 Ram Ram is one of the good cricketer one,good 2

1 Sri Sri is one of the member one 1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值