python - 每日提及一个词

标签 python pandas nlp word-count

我有以下 df,包含来自不同来源的每日文章:

print(df)

Date         content

2018-11-01    Apple Inc. AAPL 1.54% reported its fourth cons...
2018-11-01    U.S. stocks climbed Thursday, Apple is a real ...
2018-11-02    GONE are the days when smartphone manufacturer...
2018-11-03    To historians of technology, the story of the ...
2018-11-03    Apple Inc. AAPL 1.54% reported its fourth cons...
2018-11-03    Apple is turning to traditional broadcasting t...

(...)

我想计算“Apple”一词的每日提及 的总数 - 因此按日期汇总。如何创建“final_df”?

print(final_df) 

    2018-11-01    2
    2018-11-02    0
    2018-11-03    2
    (...)

最佳答案

使用count对于新的 Series,按列 df['Date']sum 聚合:

df1 = df['content'].str.count('Apple').groupby(df['Date']).sum().reset_index(name='count')
print (df1)
         Date  count
0  2018-11-01      2
1  2018-11-02      0
2  2018-11-03      2

关于python - 每日提及一个词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53813632/

相关文章:

python - 如何从一系列数组构建 Pandas 数据框

python - Pandas -> DataFrame -> 按价格排名

nlp - 主题发现/发现的最佳模型

python - 一种用于在 python 中表示语料库句子的热编码

python - 在 python 中迭代列以生成单独的图

pandas - 有效地设置行子集的值

C++ 相当于 Python difference_update?

python - 如何将列表列表中与第一个列表中的单词匹配的单词替换为与第二个列表中的第一个单词具有相同位置的单词?

python - 足球场的单应性

python - 根据共性对字符串数组进行分类