Python 当列中存在列表对象时,获取Dataframe中唯一值的计数
所以我基本上是在分析instagram账户。我已经用selenium抓取了intagram,并创建了一个datafram,其中包括到帖子的链接、喜欢的数量和使用的hashtag。因此,在数据框中,我在一个cloumn中包含了list对象,并找到了总共使用的唯一hashtag的计数。Python 当列中存在列表对象时,获取Dataframe中唯一值的计数,python,pandas,dataframe,instagram,data-analysis,Python,Pandas,Dataframe,Instagram,Data Analysis,所以我基本上是在分析instagram账户。我已经用selenium抓取了intagram,并创建了一个datafram,其中包括到帖子的链接、喜欢的数量和使用的hashtag。因此,在数据框中,我在一个cloumn中包含了list对象,并找到了总共使用的唯一hashtag的计数。 这就是thw数据帧的外观 links ... hash
这就是thw数据帧的外观
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..
我已经将hashtags存储为对应于相应帖子的列表对象。有没有更好的方法来存储hashtag?以及如何获得整体使用的唯一hashtag的计数。提前谢谢 这里有一种使用
计数器的方法:
from collections import Counter
arr = df['hashtags'].apply(pd.Series).values.ravel() # Consolidate all hashtags
count_dict = Counter(arr)
我遵循了您的方法,得到了唯一hashtag的计数,但得到的是大多数nan值。为什么呢?是因为列表的大小不同吗计数器({nan:59',#artificialintelligence':10',#machinelearning':10',#datascience':9,….``的确如此。您只需删除此键,即del count_dict[np.nan]
.Yeahh okayy!!还有一个问题!现在它返回一个计数器对象,对吗?所以我想按递减顺序对计数进行排序,并打印使用的前5个哈希标记。那么我该怎么做呢?基本上可以将其视为一个dict
,这样类似于pd.Series(count\u dict).nlargest(5)
的东西就可以了。