網(wǎng)站首頁 編程語言 正文
在處理csv文件時,會有些數(shù)據(jù)需要分組展示。
比如以下文件及統(tǒng)計效果
為了避免重復(fù)勞動,
把pandas 和?pyecharts 做了個結(jié)合
# coding=UTF-8
from pyecharts import Bar,Scatter,Line
from pyecharts import Page
import pandas as pd
# 生成的HTML文件在程序目錄 render.html
def create_line(x_data, line_data_head, line_data, line_dict):
# 建立一個Line圖返回
# x_data X 軸數(shù)據(jù)
# bar_data_head 數(shù)據(jù)列
# bar_data 數(shù)據(jù)數(shù)組二維,數(shù)量和數(shù)據(jù)列匹配, 組內(nèi)數(shù)據(jù)和 X軸數(shù)據(jù)匹配
# bar_dict 字典 , 標(biāo)題, 副標(biāo)題 , 長 , 寬
line = Line(line_dict['title'], line_dict['subtitle'], width=line_dict['width'], height=line_dict['height'])
for i in range(len(line_data_head)):
line.add(line_data_head[i], x_data, line_data[i], xaxis_interval=0, is_smooth=True)
return line
def lines_show(line_data):
# 顯示多個曲線圖
page = Page()
for b in line_data:
line = create_line(b['x'], b['head'], b['data'], b['dict'])
page.add(line)
page.render()
def create_bar(x_data, bar_data_head, bar_data, bar_dict):
# 建立一個Bar圖返回
# x_data X 軸數(shù)據(jù)
# bar_data_head 數(shù)據(jù)列
# bar_data 數(shù)據(jù)數(shù)組二維,數(shù)量和數(shù)據(jù)列匹配, 組內(nèi)數(shù)據(jù)和 X軸數(shù)據(jù)匹配
# bar_dict 字典 , 標(biāo)題, 副標(biāo)題 , 長 , 寬
bar = Bar(bar_dict['title'], bar_dict['subtitle'], width=bar_dict['width'], height=bar_dict['height'])
for i in range(len(bar_data_head)):
bar.add(bar_data_head[i], x_data, bar_data[i], xaxis_interval=0)
return bar
def bars_show(bar_data):
# 顯示多個柱狀圖
page = Page()
for b in bar_data:
bar = create_bar(b['x'], b['head'], b['data'], b['dict'])
page.add(bar)
page.render()
def csv_data_show(csv_file, x_head_key, data_key, m_yw):
# 讀取CSV 文件,獲取多列數(shù)據(jù),顯示相關(guān)圖示
df = pd.read_csv(csv_file, sep=',', encoding='gb2312')
cols_len = len(df.columns)
rows_len = len(df)
x_head = [str(c).strip() for c in df[x_head_key]]
print '數(shù)據(jù)列', cols_len, '數(shù)據(jù)行', rows_len, 'X軸數(shù)據(jù)', len(x_head), '圖數(shù)', len(data_key)
yw_list = []
for m_data in data_key:
m_list = []
m_list_head = []
for i in m_data:
di = [d for d in df[df.columns[i]]]
m_list.append(di)
m_list_head.append(df.columns[i])
yw_i = {
'x': x_head,
'head': m_list_head,
'data': m_list,
'dict': m_yw
}
yw_list.append(yw_i)
bars_show(yw_list)
# lines_show(yw_list)
def csv_data_show_comb(csv_file, x_head_key, comb_key, data_key, m_yw):
# 讀取CSV 文件,獲取單列數(shù)據(jù),分組顯示顯示相關(guān)圖示
# x_head_key X軸數(shù)據(jù)列
# comb_key 分組數(shù)據(jù)列
# data_key 顯示數(shù)據(jù)列
df = pd.read_csv(csv_file, sep=',', encoding='gb2312')
cols_len = len(df.columns)
rows_len = len(df)
m_comb = list(set([c for c in df[comb_key]]))
m_xhead = [str(d).strip() for d in df[(df[comb_key] == m_comb[0])][x_head_key]]
print '數(shù)據(jù)列', cols_len, '數(shù)據(jù)行', rows_len, 'X坐標(biāo)數(shù)據(jù)', len(m_xhead)
yw_list = []
m_list = []
m_list_head = []
for i in range(len(m_comb)):
di = [d for d in df[(df[comb_key] == m_comb[i])][data_key]]
m_list.append(di)
m_list_head.append(str(m_comb[i]))
yw_i = {
'x': m_xhead,
'head': m_list_head,
'data': m_list,
'dict': m_yw
}
yw_list.append(yw_i)
bars_show(yw_list)
# lines_show(yw_list)
def an_data1():
# 畫2張圖 : 第一季度 及 1-5月
m_data_list = [[1,2,3],[1,2,3,4,5]]
m_yw = {
'title': '工作量統(tǒng)計',
'subtitle': '',
'width': 800,
'height': 300
}
csv_data_show(r'mt_data.csv', 'S_NAME', m_data_list, m_yw)
def an_data2():
m_yw = {
'title': '工作量統(tǒng)計-分組',
'subtitle': '',
'width': 800,
'height': 300
}
csv_data_show_comb(r'mc_data.csv', 'S_NAME', 'D_MONTH', 'D_DATA', m_yw)
mc_data.csv
mt_data.csv
an_data1() 的效果
原文鏈接:https://blog.csdn.net/seakingx/article/details/80621147
相關(guān)推薦
- 2022-08-28 Redis唯一ID生成器的實(shí)現(xiàn)_Redis
- 2023-03-29 python中文字符如何轉(zhuǎn)url編碼_python
- 2022-05-27 利用Python/R語言分別解決金字塔數(shù)求和問題_python
- 2022-05-09 Python數(shù)據(jù)可視化之使用matplotlib繪制簡單圖表_python
- 2021-12-03 C/C++表格組件Qt?TableWidget應(yīng)用詳解_C 語言
- 2022-07-03 python使用open函數(shù)對文件進(jìn)行處理詳解_python
- 2022-04-15 python中yield函數(shù)的用法詳解_python
- 2021-12-18 C++?STL容器詳解之紅黑樹部分模擬實(shí)現(xiàn)_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支