gpt4 book ai didi

python - 每个 Pandas 数据帧行的词频

转载 作者:行者123 更新时间:2023-12-02 01:01:59 25 4
gpt4 key购买 nike

我试图弄清楚如何获得每个数据帧行中最常用的词 - 比如说前 10 个最常用的词。我的代码可以让我获得整个 DF 中最常用的词,但现在我需要更细化。

import pandas as pd
import numpy as np
df1 = pd.read_csv('C:/temp/comments.csv',encoding='latin-1',names=['client','comments'])
df1.head(3)

enter image description here

现在我可以得到整个 df1 中出现频率最高的词:
y = pd.Series(' '.join(df1['description']).lower().split()).value_counts()[:10]

如何获取每个 df 行的信息?

最佳答案

有几种不同的方法可以做到这一点,具体取决于您是需要数据框、字典系列还是字典列表。

from collections import Counter

# dataframe of word counts per row
res = df['comments'].str.split().apply(pd.value_counts)

# series of dictionaries of word counts, each series entry covering one row
res = df['comments'].str.split().apply(Counter)

# list of dictionaries of word counts, each list item covering one row
res = [Counter(x) for x in df['comments'].str.split()]

关于python - 每个 Pandas 数据帧行的词频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49960871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com