pandas:统计某一列字符串中特定单元出现的频率

Posted JasonLiu1919

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pandas:统计某一列字符串中特定单元出现的频率相关的知识,希望对你有一定的参考价值。

背景

某一列是字符串,想要统计该列字符串分词结果后各词出现的词频。

示例代码

# -*- coding: utf-8 -*-
# @Time    : 2022/2/13 4:18 下午
# @Author  : JasonLiu
# @FileName: test.py
import pdb
import pandas as pd
import numpy as np

df = pd.DataFrame(
    [[104472, "R.X. Yah & Co"],
    [104873, "Big Building Society"],
    [109986, "St James's Society"],
    [114058, "The Kensington Society Ltd"],
    [113438, "MMV Oil Associates Ltd"]], columns=["URN", "Firm_Name"])

# 方法1:
result1 = df.Firm_Name.str.split(expand=True).stack().value_counts()
print("方法1:")
print(result1)
# PS: str.split(expand=True).stack() is a really clever option on small data, but it quickly runs out of memory
# on data of any size. Since it expands out a matrix for every unique word in Firm_Name,
# data sparsity explodes matrix columns without many observations

print("方法2:")
result2 = pd.Series(np.concatenate([x.split() for x in df.Firm_Name])).value_counts()
print(result2)

print("方法3:")
result3 = pd.Series(' '.join(df.Firm_Name).split()).value_counts()
print(result3)

print("方法4:")
temp = df['Firm_Name'].str.cat(sep=' ')
# pdb.set_trace()
from collections import Counter
word_count = Counter(temp.split(' '))
print(word_count)

print("方法5:")
results = Counter()
df['Firm_Name'].str.split().apply(results.update)
print(results)

运行结果如下:

方法4:
Counter('Society': 3, 'Ltd': 2, 'R.X.': 1, 'Yah': 1, '&': 1, 'Co': 1, 'Big': 1, 'Building': 1, 'St': 1, "James's": 1, 'The': 1, 'Kensington': 1, 'MMV': 1, 'Oil': 1, 'Associates': 1)
方法5:
Counter('Society': 3, 'Ltd': 2, 'R.X.': 1, 'Yah': 1, '&': 1, 'Co': 1, 'Big': 1, 'Building': 1, 'St': 1, "James's": 1, 'The': 1, 'Kensington': 1, 'MMV': 1, 'Oil': 1, 'Associates': 1)

以上是关于pandas:统计某一列字符串中特定单元出现的频率的主要内容,如果未能解决你的问题,请参考以下文章

python 如何统计dataframe中某一列数据中每个元素出现的次数

关于pandas处理数据,怎么提取某一列的部分数字

如何计算我的数据中某些特定单词在 hive 中出现的频率?

pandas使用query函数删除dataframe中某一列数值大于等于某一特定值的行(remove dataframe rows baed on column value)

pandas使用query函数删除dataframe中某一列数值小于某一特定值的行(remove dataframe rows baed on column value)

jqgrid怎么统计一列的值?