Python實現(xiàn)的微信好友數(shù)據(jù)分析功能示例
本文實例講述了Python實現(xiàn)的微信好友數(shù)據(jù)分析功能。分享給大家供大家參考,具體如下:
這里主要利用python對個人微信好友進(jìn)行分析并把結(jié)果輸出到一個html文檔當(dāng)中,主要用到的python包為itchat,pandas,pyecharts等
1、安裝itchat 微信的python sdk,用來獲取個人好友關(guān)系。獲取的代碼 如下:
import itchat import pandas as pd from pyecharts import Geo, Bar itchat.login() friends = itchat.get_friends(update=True)[0:] def User2dict(User): User_dict = {} User_dict["NickName"] = User["NickName"] if User["NickName"] else "NaN" User_dict["City"] = User["City"] if User["City"] else "NaN" User_dict["Sex"] = User["Sex"] if User["Sex"] else 0 User_dict["Signature"] = User["Signature"] if User["Signature"] else "NaN" User_dict["Province"] = User["Province"] if User["Province"] else "NaN" return User_dict friends_list = [User2dict(i) for i in friends] data = pd.DataFrame(friends_list) data.to_csv('wechat_data.csv', index=True)
2、對獲取到的數(shù)據(jù)進(jìn)行分析。
主要分析了男女比例,以及好友所在城市分布,并且在地圖上面展示了微信好友的分布情況。另外其他的數(shù)據(jù)讀者可以自己去分析,這里只是提供一個引導(dǎo)而已。
import pandas as pd from pyecharts import Geo, Bar def Cal_mVw(data): result = {} for i in data: if i == 1: result["man"] = result.get("man", 0) + 1 elif i == 2: result["woman"] = result.get("woman", 0) + 1 else: result["unknown"] = result.get("nunknown", 0) + 1 return result def count_city(data): result = {} for i in data: if data is not "NaN" or data is not "nan": result[i] = result.get(i, 0) + 1 return result data1 = pd.read_csv('wechat_data.csv', encoding='GBK') manVSwoman=Cal_mVw(data1["Sex"]) #print(manVSwoman) bar = Bar("個人微信好友男女比例") bar.add("男女人數(shù)", ["男", "女", "不詳"], [139, 75, 1]) bar.render() city=count_city(data1["City"]) geo = Geo("微信好友分布", "", title_color="#fff", title_pos="center", width=1200, height=600, background_color='#404a59') #attr, value = geo.cast(city) geo.add("", city.keys(), city.values(), visual_range=[0, 30], visual_text_color="#fff", symbol_size=15, is_visualmap=True) geo.show_config() geo.render()
男女比例畫出來的圖如下所示
獲取到的好友分布情況如下圖所示:
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
VSCode2022配置Python3.9.6的詳細(xì)教程
這篇文章主要介紹了VSCode2022配置Python3.9.6教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09Anaconda和ipython環(huán)境適配的實現(xiàn)
這篇文章主要介紹了Anaconda和ipython環(huán)境適配的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Python?第三方opencv庫實現(xiàn)圖像分割處理
這篇文章主要介紹了Python?第三方opencv庫實現(xiàn)圖像分割處理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06