为了拿pandas练手,做了这个东西,分享一下给大家,有需要的可以参考,不喜勿喷
1.使用wxBot获取到微信好友列表
2.下载好wxBot后根据文档提示运行起来,登录微信
3.在当前目录下 找到 temp/contact_list.json
4.在 json-csv网站 把Json文件转换成CSV文件
# -*- coding: utf-8 -*-
#导入库
import re
import pandas as pd
import matplotlib.pyplot as plt
import jieba.analyse
from snownlp import SnowNLP
from wordcloud import WordCloud
def readFile(filePath):
return pd.read_csv(filePath)
if __name__ == "__main__":
#Mac下设置Plot的中文显示,如果不设置,中文就显示成方块
plt.rcParams['font.sans-serif']=['SimHei']
df = readFile('./contact_list.csv')
#处理空数据设置成默认数据
df.Province= df.Province.fillna('未知')
df.City= df.City.fillna('未知')
showAreaBar(df,'Province','按省分布',5)
showAreaBar(df,'City','按市分布',3)
showGenderPie(df)
analyseSignature(df['Signature'])
展示地区分布的柱状图,按City分布的截图就不放上去了
def showAreaBar(datas, area, title, limit):
plt.figure(figsize=(8,10))
area_group = datas['PYQuanPin'].groupby(datas[area])
name_list = []
num_list = []
less = 0
lessLimit = limit
for name, group in area_group:
if group.size < lessLimit:
less += group.size
else:
name_list.append(name)
num_list.append(group.size)
name_list.append('少于{}人'.format(lessLimit))
num_list.append(less)
# plt.subplot(221)
plt.title(title)
plt.barh(range(len(num_list)), num_list,color='rgb',tick_label=name_list)
#必须要先Save
plt.savefig(area + ".jpg")
plt.show()
plt.title('我的微信好友地区分布')
plt.pie(x=num_list, labels=name_list,autopct='%3.1f %%',
shadow=False, labeldistance=1.2, startangle = 90,pctdistance = 0.6
)
plt.savefig(area + '_Pie.jpg')
plt.show()
好友中性别比例的饼状图
def showGenderPie(datas,):
# print (df[df.Sex == 2])
males = datas[datas.Sex == 1]
females = datas[datas.Sex == 2]
unknowns = datas[datas.Sex == 0]
labels = ['男', '女', '未知']
fracs = [males.size, females.size,unknowns.size]
explode = [0, 0,0.1] # 0.1 凸出这部分,
plt.title('我的微信好友性别比例')
plt.pie(x=fracs, labels=labels,explode=explode,autopct='%3.1f %%',
shadow=False, labeldistance=1.1, startangle = 90,pctdistance = 0.6
)
plt.savefig('gender.jpg')
plt.show()
微信好友签名信息情感分析
def analyseSignature(signatureList):
signatures = ''
emotions = []
for signature in signatureList:
if(isinstance(signature,str) and signature != None):
signature = signature.strip().replace('span', '').replace('class', '').replace('emoji', '')
signature = re.sub(r'1f(\d.+)','',signature)
if(len(signature)>0):
nlp = SnowNLP(signature)
emotions.append(nlp.sentiments)
signatures += ' '.join(jieba.analyse.extract_tags(signature,5))
with open('signatures.txt','wt',encoding='utf-8') as file:
file.write(signatures)
# Sinature WordCloud
# back_coloring = np.array(Image.open('flower.jpg'))
font_path="/System/Library/fonts/PingFang.ttc"
wordcloud = WordCloud(
font_path=font_path,
background_color="white",
max_words=1200,
# mask=back_coloring,
max_font_size=75,
random_state=45,
width=960,
height=720,
margin=15
)
wordcloud.generate(signatures)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
wordcloud.to_file('signatures.jpg')
# Signature Emotional Judgment
count_good = len(list(filter(lambda x:x>0.66,emotions)))
count_normal = len(list(filter(lambda x:x>=0.33 and x<=0.66,emotions)))
count_bad = len(list(filter(lambda x:x<0.33,emotions)))
labels = [u'负面消极',u'中性',u'正面积极']
values = (count_bad,count_normal,count_good)
plt.rcParams['font.sans-serif'] = ['simHei']
plt.rcParams['axes.unicode_minus'] = False
plt.xlabel(u'情感判断')
plt.ylabel(u'频数')
plt.xticks(range(3),labels)
plt.legend(loc='upper right',)
plt.bar(range(3), values, color = 'rgb')
plt.title(u'微信好友签名信息情感分析')
plt.show()