gpt4 book ai didi

python - 如何计算 Pandas Dataframe 中的词频 - Python

转载 作者:行者123 更新时间:2023-11-28 22:29:20 27 4
gpt4 key购买 nike

我目前根据字典创建了一个 Pandas Dataframe。数据框看起来像:

      URL         TITLE
0 /xxxx.xx Hi this is word count
1 /xxxx.xx Hi this is Stack Overflow
2 /xxxx.xx Stack Overflow Questions

我想在此表中添加一个新列,其中列出了单词“Stack Overflow”出现的频率。因此,例如,它会像:

      URL         TITLE                          COUNT
0 /xxxx.xx Hi this is word count 0
1 /xxxx.xx Hi this is Stack Overflow 1
2 /xxxx.xx Stack Overflow Questions 1

count 函数似乎不适用于字典,但仅适用于字符串。有没有简单的方法可以做到这一点?

最佳答案

假设这实际上是一个 pandas dataframe,您可以这样做:

import pandas as pd

table = { 'URL': ['/xxxx.xx', '/xxxx.xx', '/xxxx.xx'],
'TITLE': ['Hi this is word count', 'Hi this is Stack Overflow', 'Stack Overflow Questions']}

df = pd.DataFrame(table)
df['COUNT'] = df.TITLE.str.count('Stack Overflow')
print(df)

这会产生:

                       TITLE       URL  COUNT
0 Hi this is word count /xxxx.xx 0
1 Hi this is Stack Overflow /xxxx.xx 1
2 Stack Overflow Questions /xxxx.xx 1

关于python - 如何计算 Pandas Dataframe 中的词频 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43087420/

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