某次做一个数据的处理,要计算用户的粉丝数量,数据集大概是这样的:
传播节点微博用户id | 关注用户ids |
---|---|
ae26e5e3db7626dcaf6819ce5492d534 | “04e9dc04d4b600d574d67b298a7dea7d,···” |
a845733e3729a136889c07d275bcc3c5 | “aebe49645667a02eae6ab6734ade24eb,···” |
68e605feb5344fd413587b4245946c24" | 77c471d3aba195b1322800602a93dc72,···" |
这里的数据,都是经过脱敏处理后的id,即每个用户和他们的关注列表。“关注用户ids”应该是字符串类型,每一行由双引号包裹,由逗号作为id之间的分隔符。要计算用户的粉丝数量,就是看他们在所有用户的关注列表当中出现了多少次,也就是要对“关注用户ids”列出现的各个id进行计数。
参考博文1进行以下处理
countN = dataI0['name'].str.split('|', expand=True).stack().value_counts()
# 计算各元素出现的次数
其中,不能对数据框的列Chart3Part[‘关注用户ids’]直接应用split,而需要先调用str。
- 其中的expand=True是按逗号对每一行进行分割后,将其扩展成多列。
- stack()则是构造二级行索引,在原本的行索引上,将列作为二级行索引。可参考博文2,也可见下文当中的实验三
- value_counts()是对值出现的次数进行计数,其返回值是一个pd.Series,name为被计数的列的名字,index为被计数的项,值为出现的次数
博文1当中还有另一种方法,先单独对每一行进行处理,再从总的视角进行计数
countN = pd.Series(Counter([y for x in dataI0['name'] for y in x.split('|')]))
完整的示例如下:
import pandas as pd
dataI0 = pd.DataFrame(data=["book","fish","icecream|book","fish","campfire|book"],columns=["name"])
print(dataI0)
# 创建数据框
# name
# 0 book
# 1 fish
# 2 icecream|book
# 3 fish
# 4 campfire|book
# 方法一----------------------------------------------------------------------------------------
countN = dataI0['name'].str.split('|', expand=True).stack().value_counts()
print(countN)
# 对name列进行计数
# book 3
# fish 2
# icecream 1
# campfire 1
# dtype: int64
# 方法二---------------------------------------------------------------------------------------
from collections import Counter
countN = pd.Series(Counter([y for x in dataI0['name'] for y in x.split('|')]))
print(countN)
# book 3
# fish 2
# icecream 1
# campfire 1
# dtype: int64
# 实验一---------------------------------------------------------------------------------------
countN = dataI0['name'].str.split('|')
print(countN)
# 此处是对每一行的数据按照'|'进行分割,每一行返回的是分割后的数组
# 0 [book]
# 1 [fish]
# 2 [icecream, book]
# 3 [fish]
# 4 [campfire, book]
# Name: name, dtype: object
# 实验二---------------------------------------------------------------------------------------
countN = dataI0['name'].str.split('|', expand=True)
print(countN)
# expand=True,即分割后扩展为多列,不足之处使用None补充
# 0 1
# 0 book None
# 1 fish None
# 2 icecream book
# 3 fish None
# 4 campfire book
# 实验三---------------------------------------------------------------------------------------
countN = dataI0['name'].str.split('|', expand=True).stack()
print(countN)
# stack(),即将列变为二级行索引,原行索引为一级行索引,也就是说现在的数据框只有一列了
# 所有的数据都在这一列,直接对这一列进行计数即可
# 0 0 book
# 1 0 fish
# 2 0 icecream
# 1 book
# 3 0 fish
# 4 0 campfire
# 1 book
# dtype: object
# 实验四---------------------------------------------------------------------------------------
countN = [y for x in dataI0['name'] for y in x.split('|')]
print(countN)
# 这里的思路是利用列表推导式,对每一行按照'|'进行分割后,对分割的结果进行枚举,最后得到一个列表
# 所有的结果都在列表当中
# ['book', 'fish', 'icecream', 'book', 'fish', 'campfire', 'book']
# 实验五---------------------------------------------------------------------------------------
countN = Counter([y for x in dataI0['name'] for y in x.split('|')])
print(countN)
# 博文的思路是对列表应用Counter,直接计数
# 之后再转换成为pd.Series
# Counter({'book': 3, 'fish': 2, 'icecream': 1, 'campfire': 1})
# 实验六---------------------------------------------------------------------------------------
countN = pd.Series([y for x in dataI0['name'] for y in x.split('|')])
print(countN)
# 也可以直接转换成pd.Series,所有的数据都在这一列了
# 效果与上文stack()后的结果相似
# 0 book
# 1 fish
# 2 icecream
# 3 book
# 4 fish
# 5 campfire
# 6 book
# dtype: object
# 实验七---------------------------------------------------------------------------------------
countN = pd.Series([y for x in dataI0['name'] for y in x.split('|')]).value_counts()
print(countN)
# 在pd.Series的基础上,也可以直接使用value_counts()
# book 3
# fish 2
# icecream 1
# campfire 1
# dtype: int64
版权声明:本文为weixin_52202311原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。