在 pandas 数据帧中计算某些词的出现次数

编程入门 行业动态 更新时间:2024-10-17 07:35:11
本文介绍了在 pandas 数据帧中计算某些词的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想计算一个数据帧中某些字的出现次数。我知道使用str.contains

I want to count number of occurrences of certain words in a data frame. I know using "str.contains"

a = df2[df2['col1'].str.contains("sample")].groupby('col2').size() n = a.apply(lambda x: 1).sum()

目前我正在使用上述代码。有没有一种匹配正则表达式并获得事件计数的方法?在我的情况下,我有一个大数据框,我想匹配大约100个字符串。

Currently I'm using the above code. Is there a method to match regular expression and get the count of occurrences? In my case I have a large dataframe and I want to match around 100 strings.

推荐答案

str .contains 方法接受正则表达式:

The str.contains method accepts a regular expression:

Definition: df.words.str.contains(self, pat, case=True, flags=0, na=nan) Docstring: Check whether given pattern is contained in each string in the array Parameters ---------- pat : string Character sequence or regular expression case : boolean, default True If True, case sensitive flags : int, default 0 (no flags) re module flags, e.g. re.IGNORECASE na : default NaN, fill value for missing values.

例如:

In [11]: df = pd.DataFrame(['hello', 'world'], columns=['words']) In [12]: df Out[12]: words 0 hello 1 world In [13]: df.words.str.contains(r'[hw]') Out[13]: 0 True 1 True Name: words, dtype: bool In [14]: df.words.str.contains(r'he|wo') Out[14]: 0 True 1 True Name: words, dtype: bool

要计算出现的结果,您可以将此布尔值系列:

To count the occurences you can just sum this boolean Series:

In [15]: df.words.str.contains(r'he|wo').sum() Out[15]: 2 In [16]: df.words.str.contains(r'he').sum() Out[16]: 1

更多推荐

在 pandas 数据帧中计算某些词的出现次数

本文发布于:2023-11-30 17:30:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:次数   数据   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!