【发布时间】:2022-01-04 15:47:24
【问题描述】:
关于上一个问题,我希望遍历字典列表并将输出转换为新的数据框。现在,我有一个包含两列的 CSV。一列带有单词,另一列带有 URL(见下文)。
| Keyword | URL |
| -------- | -------------- |
| word1 | www.example.com/topic-1 |
| word2 | www.example.com/topic-2 |
| word3 | www.example.com/topic-3 |
| word4 | www.example.com/topic-4 |
我已将此 CSV 文件转换为字典列表,并尝试遍历这些列表以计算单词在 URL 上显示的频率。
我的代码可以在this colab notebook看到。
我希望最终输出如下所示:
| Keyword | URL | Count |
|:---- |:------: | -----:|
| word1 | www.example.com/topic-1 | 1003 |
| word2 | www.example.com/topic-2 | 405 |
| word3 | www.example.com/topic-3 | 123 |
| word4 | www.example.com/topic-4 | 554 |
'Count' 列是 'word1' 在 'www.example.com/topic-1' 上出现的频率。
感谢任何帮助!
【问题讨论】:
-
“我已将此 CSV 文件转换为字典列表” 如何?字典列表是什么样的?
-
或者这个,它解释了如何使用
DataFrame.apply:pandas create new column based on values from other columns / apply a function of multiple columns, row-wise 创建一个新列。另见the documentation for DataFrame.apply。
标签: python pandas dictionary beautifulsoup word-count