定义一个函数,使用Python返回一段时间内最常用的单词

2024-05-23 17:42:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的数据帧如下所示:

    text                    senders date                   words    characters
0   esta muy bueno          L       2018-03-14 17:00:00    4        20
1   congratz                L       2018-03-14 17:00:00    1        8
2   en la tarde instalamos  L       2018-03-14 17:00:00    5        28
3   muy bueno eso..         G       2018-03-14 17:00:00    5        28
4   asi voy a hacer         G       2018-03-14 17:00:00    9        39
5   jajaja                  L       2018-03-14 17:00:00    1        6
6   jajajajajajaj           G       2018-03-14 17:16:00    1        13
7   lucho                   G       2018-03-14 17:33:00    1        5
8   ah no                   G       2018-03-14 17:33:00    2        5
9   nonono                  G       2018-03-14 17:33:00    1        6

我想做的是定义一个函数,返回text列中的某个单词在某个时间段内被每个senders(L或G)使用的次数。这可能吗?数据从2018年3月到2018年12月。你知道吗

我已经编写了一段代码,返回text列中最常见的单词:

from collections import Counter 
words = '' for i in df.text.values: 
    words += '{} '.format(i.lower()) #make words lowercase 
pd.DataFrame(Counter(words.split()).most_common(20), columns=['word', 'frequency'])

此外,date列已被转换为datatime,具有:

df['date'] = pd.to_datetime(df['date'])

Tags: 数据textdfdatecounter单词enpd