最近学习了使用Power BI、pandas相关知识,一直没有什么好的想法来实践一下,看了下之前写的 一片查看微信好友男女比例的文章觉得还可以再继续扩展一下,于是乎有了下文,废话不多说,直接开始。
环境准备:Power BI软件、pandas、itchat、plotly模块,各位可自行百度安装。
大致流程:step1 获取微信好友数据 step2 清洗、整理数据 step3 使用Power BI、plotly实现数据可视化。
step1 获取微信好友数据,代码如下:
import itchat #导入itchat
login = itchat.login() #微信登录
friends = itchat.get_friends()
print(friends) #获取好友数据并打印结果
结果如图所示:我们获取的数据是一个list型,我们要的数据是从第二个开始的,这些数据内容包括了微信号有的昵称、性别、所在城市、省份、备注名称、昵称、座右铭等等,然后从中获取我需要的数据,并以字典形式保存。代码如下:
data = {} #新建一个空字典 '''这里新建的几个空列表并准备作为data中的value值''' IsOwner = [] HideInputBarFlag = [] Sex = [] Province = [] RemarkName = [] PYQuanPin = [] City = [] Signature = [] NickName = []
for i in friends[1:]: #print(i) IsOwner.append(i['IsOwner']) HideInputBarFlag.append(i['HideInputBarFlag']) Sex.append(i['Sex']) Province.append(i['Province']) RemarkName.append(i['RemarkName']) PYQuanPin.append(i['PYQuanPin']) City.ap