python - 当列中有列表对象时,获取熊猫数据框中唯一值的计数
问题描述
所以基本上我正在尝试分析 Instagram 帐户。我已经使用 selenium 抓取了 intagram 并创建了一个数据帧,其中包括指向帖子的链接、喜欢的数量和使用的主题标签。因此,在数据框中,我将列表对象包含在一个 cloumn 中,并且我想找到总共使用的唯一主题标签的计数。
这就是数据框的样子。
links ... hashtags
0 https://www.instagram.com/p/CLrU5s5g7L7/ ... [#data, #datascience, #technology, #machinelea...
1 https://www.instagram.com/p/CLojnLQgEVs/ ... [#datascience, #machinelearning, #python, #art...
2 https://www.instagram.com/p/CLjhzPxgpkM/ ... [#python, #AI, #ML, #artificialintelligence, #...
3 https://www.instagram.com/p/CLgUsXAgOah/ ... [#datascience, #machinelearning, #python, #art...
4 https://www.instagram.com/p/CLdfVBHAibb/ ... [#billgates, #softwareengineering, #softwareen...
5 https://www.instagram.com/p/CLbGqrYgl74/ ... [#python3, #python, #pythonprogramming, #AI, #...
6 https://www.instagram.com/p/CLZKOEcg72M/ ... [#python3, #python, #pythonprogramming, #AI, #...
7 https://www.instagram.com/p/CLYe9AJgg0U/ ... [#datascience, #machinelearning, #python, #art...
8 https://www.instagram.com/p/CLV4UP5Af-2/ ... [#pawrihoraihai, #programming, #coding, #progr...
9 https://www.instagram.com/p/CLTSxc5g2cJ/ ... [#datascience, #machinelearning, #python, #art..
我已将主题标签存储为与相应帖子相对应的列表对象。有没有更好的方法来存储主题标签?以及如何获取整体使用的唯一主题标签的数量。
提前致谢!!
解决方案
这是一种使用方法Counter
:
from collections import Counter
arr = df['hashtags'].apply(pd.Series).values.ravel() # Consolidate all hashtags
count_dict = Counter(arr)
推荐阅读
- c - 如何将代码添加到 SWIG 生成的 C 包装函数(用于 Python)
- mongodb - 将字符串数组存储到 mongoDB 集合中
- python - 连接两个numpy数组时的`TypeError:无效类型提升`
- python - 如何根据条件过滤 JSON 文件?
- javascript - (点击事件)切换和隐藏其他 JavaScript
- flutter - 如何在flutter_platform_widgets中使用抽屉:^0.72.0
- java - Java - 取两个字符串并组合成一个日期对象
- python - 如何在 OpenCV 中逼近具有固定角度多边形的形状?
- java - 为什么我的图形点不显示?(Java 绘图问题)
- hyperledger-fabric - 无法运行对等