您的位置:首页 > 编程语言 > Python开发

Python对EXCEL中关键字相同的记录进行统计,并对前若干个数据进行柱状图展示

2020-02-02 17:39 681 查看

Python对EXCEL中关键字相同的记录进行统计,并对前若干数据进行柱状图展示
import csv
import matplotlib.pyplot as plt
import numpy as np
import pandas
import os
source=“D:/乔雪梅/钢构用户行为分析”
file_list=os.listdir(source) #读取某一路径下的所有文件

#存储全量路径
source_m=[ source+"/"+i for i in file_list]
#正常显示读取的表格中的中文字体
plt.rcParams[‘font.sans-serif’]=[‘SimHei’]
plt.rcParams[‘axes.unicode_minus’] = False

def sta(fp):
#打开文件
file =open(fp,‘r’)
#读取文件中的所有行
lines=file.readlines()
file.close()

row=[]#定义行数组
column=[]#存储功能码
col=[]#存储功能名
for line in lines:
row.append(line.split(','))
for i in range(1,len(row)):
m=row[i]
column.append(m[2])#获取功能码
col.append(m[3])   #获取功能名

#统计每个功能点击的次数,按功能码进行统计
click_numbers={}
for it in column:
click_numbers[it] = 0
for i in column:
click_numbers[i]=click_numbers[i]+1

fig = plt.figure()
#截取部分功能(使用次数多的功能)显示
function_name=[]
click_times=[]
for ms in click_numbers.keys():
if len(click_numbers.keys())>11:
if click_numbers[ms]>5:
if col[column.index(ms)]=='"查看型钢代号表"' or  col[column.index(ms)]=='"新建"'or col[column.index(ms)]=='" ..."' or col[column.index(ms)]=='"动态"':
#print(col[column.index(ms)])
function_name.append(col[column.index(ms)]+ms)
else:
function_name.append(col[column.index(ms)])
click_times.append(click_numbers[ms])
else:
if col[column.index(ms)]=='"新建"':
function_name.append(col[column.index(ms)]+ms)
else:
function_name.append(col[column.index(ms)])

click_times.append(click_numbers[ms])

#删除特殊分组中的功能
delete=[’“软件关闭”’,’“关闭工程”’,’“新建工程”’,’“软件启动”’,’“保存工程”’,’“打开工程”’,’“加密锁-启动”’]
for d in delete:
if d in function_name:
indexs=function_name.index(d)
function_name.remove(d)
#print(“delete”,click_times[indexs],indexs)
click_times.remove(click_times[indexs])

plt.bar(function_name,click_times,0.1,color="green")
plt.xlabel("功能", fontsize=20)
plt.ylabel('点击次数', fontsize=20)
plt.title(file_list[source_m.index(fp)])
#前两个参数确定柱子的位置,1.02*p代表在柱子顶稍高一点,第三个参数设定显示数据,fontsize规定字号

#显示功能名
for x, y in enumerate(click_times):
plt.text(x, y*1.02 , '%s' % y, ha='center', va='bottom')

return function_name

total=[]
fp=source_m[6] #7个用户,每个用户单个调试 0-6
sta(fp)
plt.show()

结果如下图:

  • 点赞
  • 收藏
  • 分享
  • 文章举报
weixin_40763786 发布了9 篇原创文章 · 获赞 0 · 访问量 1118 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: