首页 > 解决方案 > 当列中有列表对象时,获取熊猫数据框中唯一值的计数

问题描述

所以基本上我正在尝试分析 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..

我已将主题标签存储为与相应帖子相对应的列表对象。有没有更好的方法来存储主题标签?以及如何获取整体使用的唯一主题标签的数量。
提前致谢!!

标签: pythonpandasdataframeinstagramdata-analysis

解决方案


这是一种使用方法Counter

from collections import Counter

arr = df['hashtags'].apply(pd.Series).values.ravel()  # Consolidate all hashtags
count_dict = Counter(arr)

推荐阅读