Python 分析在德的中国程序员,告别 996?(5)
数据分析
开发需求
- 生成话题词云
- 分析消息种类占比
- 分析日均聊天曲线
- 分析群员聊天活跃时间热点图
开发分解
1. 话题词云
相关代码和上面相近,生成的云图:

分析:
德国中国两者工作生活的对比是永恒的话题,到底回国还是留德,经常是热点。
因为是职业群,所以大部分话题还是集中在职场:公司、工作、老板、工资、技术
IT领域不得不提领头羊美国,包括硅谷的工资。
讨论贸易战少不了华为
创业目前在留德华中也是个热门话题
2. 消息种类占比,生成Bar Chart
def gen_bar_plot_msg_type(self, csv_file):
df = pd.read_csv(csv_file, delimiter=' ', encoding='utf-8')
df['msg_type'].value_counts().plot(kind='bar')
plt.subplots_adjust(bottom=0.2)
plt.title('Message Type [%s - %s]' % (self.fl_days[0], self.fl_days[1]))
path_image = os.path.join(self.path_analyse,
'%s_chat_msg_type_bar_%s_%s.png' % (self.group_id, self.fl_days[0], self.fl_days[1]))
plt.savefig(path_image)
plt.close()
return path_image
分析:
聊天以文字信息为主,没有出现其他灌水群的斗图行为。
3. 日均聊天频率,生成Bar Chart
def gen_bar_plot_chat_freq_day(self, csv_file):
df = pd.read_csv(csv_file, delimiter=' ', encoding='utf-8')
msg_count = len(df)
time_list = self.cal_time_list_chat_freq_day(df)
plt.figure(figsize=(18, 9))
plt.bar(time_list.keys(), time_list.values(), width=.8, facecolor='lightskyblue', edgecolor='white')
plt.xticks(range(len(time_list)), time_list.keys())
for x_axies in time_list:
y_axies = time_list[x_axies]
label = '{}%'.format(round(y_axies*1.0/msg_count*100, 2))
plt.text(x_axies, y_axies+0.05, label, ha='center', va='bottom')
plt.title('Chat frequency in 24 hours [%s - %s]' % (self.fl_days[0], self.fl_days[1]))
path_image = os.path.join(self.path_analyse,
'%s_chat_freq_day_bar_%s_%s.png' % (self.group_id, self.fl_days[0], self.fl_days[1]))
plt.savefig(path_image)
plt.close()
return path_image
分析:
- 每日从六点开始活跃,估计是一部分人上班通勤坐车时有时间聊聊
- 早七八点到公司开始工作,安静
- 早九、十点开始活跃,到午休11点左右到达高峰
- 午休后工作时间
- 下午三点开始活跃,这时是德企里的下午茶时间
- 晚9点饭后再次活跃一下
4. 群员聊天活跃时间周热点图, 输出Heat Map
def gen_heatmap_member_activity(self, csv_file): df = pd.read_csv(csv_file, delimiter=' ', encoding='utf-8') create_times = df['create_time'] week_online = [[0 for j in range(24)] for i in range(7)] for li in create_times: week_online[int(mk_datetime(li, "%Y-%m-%d %H:%M:%S").weekday())][int(li[11:13])] += 1 week_online = np.array([li for li in week_online]) columns = [str(i) + '-' + str(i + 1) for i in range(0, 24)] index = ['Mon.', 'Tue.', 'Wed.', 'Thu.', 'Fri.', 'Sat.', 'Sun.'] week_online = pd.DataFrame(week_online, index=index, columns=columns) plt.figure(figsize=(18.5, 9)) plt.rcParams['font.sans-serif'] = ['SimHei'] sns.set() # Draw a heatmap with the numeric values in each cell sns.heatmap(week_online, annot=True, fmt="d", cmap="YlGnBu") path_image = os.path.join(self.path_analyse, '%s_activity_heatmap_%s_%s.png' % (self.group_id, self.fl_days[0], self.fl_days[1])) plt.savefig(path_image, format='png', dpi=300) plt.close() return path_image

分析:
- 周一大家都很忙,或者装着很忙的样子
- 周二下午开始活跃了
- 周三上午也活跃起来
- 周四,快到周末了,放松,全天活跃
- 周五,上午欢乐时光,下午和德国同事一样,走的走跑的跑
- 周末死一般沉寂
从这个分析图可以看出,中国程序员上班是非常用心和责任感的,同时也非常遵守德企工作时间相关制度,坚决不加班,坚决朝九晚五。
996是什么?能吃吗?
认真地说,为工作和任务有限加班是可以的,但我非常反对无效的为加班而加班,把996作为KPI考勤标准的做法。
德国大中型企业一般做法是员工自行调配加班时间,某段时间任务紧,加班时间多了的话可以将超时存起来,之后再换成休假;实在没空休假的可以换成工资,不过一般HR和工会不推荐这么做,超时太多将强制休假—-员工健康比工作重要。
在德企小公司或咨询公司里,就不一定了,因为可能业绩和分红挂钩,或者小公司项目紧张,主动或被动加班是很常见的。
绝大多数IT企业并不固定员工的上下班时间,而是采用核心时间制度,比如10-15这五个小时员工必须在岗,但员工可以自行决定上班和下班时间,早来早走,晚来晚走,自由度高。
制作PDF总报表
以上数据分析步骤生成了若干独立的图片报表,不便传阅,可以将其集中整理并且排版格式化到一个PDF总报表里,方便阅读。
所需第三方库如下,可以将含图片的html页面完整输出成PDF文件。
pip3 install pdfkit Install wkhtmltopdf Debian/Ubuntu: > sudo apt-get install wkhtmltopdf Redhat/CentOS > sudo yum install wkhtmltopdf MacOS > brew install Caskroom/cask/wkhtmltopdf
开发分解
1. 准备HTML模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
h1 {
text-align: center;
}
h2 {
text-align: center;
margin-top: 20px;
}
img {
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<h1>{{group_name}} 聊天数据分析</h1>
<h2>{{date_begin}} - {{date_end}}</h2>
<h2>24小时内聊天频率</h2>
<img src="{{img_chat_freq_day}}" style="width:100%;"/>
<h2>消息类型</h2>
<img src="{{img_chat_msg_type}}" style="width:80%;"/>
<h2>日均聊天数量</h2>
<img src="{{img_chat_count_day}}" style="width:100%;"/>
<h2>群友活跃时间热点图</h2>
<img src="{{img_chat_heating_act}}" style="width:100%;"/>
<div class="divider_b"></div>
......2. 生成PDF
读取HTML模板,替换Pattern,生成PDF
with open('./assets/chat_analysis_%s.html' % lang, 'r') as file:
file_data = file.read()
# 替换Pattern
file_data = file_data.replace('{{date_begin}}', self.fl_days[0])
file_data = file_data.replace('{{date_end}}', self.fl_days[1])
file_data = file_data.replace('{{img_chat_history}}', Path(img_chat_history).name)
file_data = file_data.replace('{{img_chat_freq_day}}', Path(img_chat_freq_day).name)
......
# 输出临时HTML文件
with open(path_html, 'w') as file:
file.write(file_data)
# 输出pdf
pdfkit.from_file(path_html, path_pdf)3. 定时任务
添加定时任务,每月第一天早八点自动启动数据分析任务,分析上个月数据,然后自动将PDF报表发到群里。
# 08:10am at the first day of the month scheduler.add_job(lambda: process_schedule(bot_db, bot, group_1), 'cron', month='1-12', day=1, hour=8, minute=1, timezone="Europe/Paris") # 发送文件到指定群里 group.send_file(file_path)
最终PDF报表预览:

总结
通过数据分析可以非常直观地了解工作和生活在德国的中国程序员们,平时做什么工作,说什么话题。不过因为采样数量较小,某些分析无法采用更明确的类别数量占比图,分析结果难免有偏差,还请见谅。
本文使用Python和相关库快速完成了数据采集,清洗和分析的工作,你可以基于该项目,扩展自己的数据分析模块,比如图灵聊天机器人,连接各类第三方服务。