Python面向?qū)ο髮?shí)現(xiàn)數(shù)據(jù)分析的實(shí)例詳解
案例
某公司,有2份數(shù)據(jù)文件,現(xiàn)需要對(duì)其進(jìn)行分析處理,計(jì)算每日的銷售額并以柱狀圖表的形式進(jìn)行展示。
需求分析
實(shí)現(xiàn)步驟
- 設(shè)計(jì)一個(gè)類,可以完成數(shù)據(jù)的封裝
- 設(shè)計(jì)一個(gè)抽象類,定義文件讀取的相關(guān)功能,并使用子類實(shí)現(xiàn)具體功能
- 讀取文件,生產(chǎn)數(shù)據(jù)對(duì)象
- 進(jìn)行數(shù)據(jù)需求的邏輯計(jì)算(計(jì)算每一天的銷售額)
- 通過PyEcharts進(jìn)行圖形繪制
代碼
實(shí)例1
""" 數(shù)據(jù)定義的類 """ class Record: def __init__(self,data,order_id,money,province): self.data=data self.order_id=order_id self.money=money self.province=province def __str__(self): return f"{self.data},{self.order_id},{self.money},{self.province}"
實(shí)例2
""" 和文件相關(guān)的類定義 """ import json from data_define import Record class FileReader: def read_data(self): #讀取文件的數(shù)據(jù),讀取到的每一條數(shù)據(jù)都轉(zhuǎn)換為Record對(duì)象,將它們都封裝到list內(nèi)返回即可 pass class TextFileReader(FileReader): def __init__(self,path): self.path=path #復(fù)寫(實(shí)現(xiàn)抽象方法)父類的方法 def read_data(self): f=open(self.path,"r",encoding="utf-8") record_list=[] for line in f.readlines(): line=line.strip()#消除讀取到的每一行數(shù)據(jù)中的“\n” data_list=line.split(",") record=Record(data_list[0],data_list[1],int(data_list[2]),data_list[3]) record_list.append(record) f.close() return record_list class JsonFileReader(FileReader): def __init__(self,path): self.path=path #復(fù)寫(實(shí)現(xiàn)抽象方法)父類的方法 def read_data(self): f=open(self.path,"r",encoding="utf-8") record_list=[] for line in f.readlines(): data_dict=json.loads(line) record=Record(data_dict["date"],data_dict["order_id"],int(data_dict["money"]),data_dict["province"]) record_list.append(record) f.close() return record_list if __name__ == '__main__': text_file_reader=TextFileReader("D:/2011年1月銷售數(shù)據(jù).txt") list1=text_file_reader.read_data() for l in list1: print(l) print("========================================================================") json_file_reader=JsonFileReader("D:/2011年2月銷售數(shù)據(jù)JSON.txt") list2=json_file_reader.read_data() for l in list2: print(l)
實(shí)例3
from pyecharts.charts import Bar from pyecharts.options import * from pyecharts.globals import * from file_define import FileReader,TextFileReader,JsonFileReader from data_define import Record text_file_reader=TextFileReader("D:/2011年1月銷售數(shù)據(jù).txt") json_file_reader=JsonFileReader("D:/2011年2月銷售數(shù)據(jù)JSON.txt") jan_data=text_file_reader.read_data() feb_data=json_file_reader.read_data() all_data:list[Record]=jan_data+feb_data #開始進(jìn)行數(shù)據(jù)計(jì)算 data_dict={} for record in all_data: if record.data in data_dict.keys(): data_dict[record.data]+=record.money else: data_dict[record.data]=record.money #可視化 bar = Bar(init_opts=InitOpts(theme=ThemeType.LIGHT)) bar.add_xaxis(list(data_dict.keys())) bar.add_yaxis("銷售額",list(data_dict.values()),label_opts=LabelOpts(is_show=False)) bar.set_global_opts( title_opts=TitleOpts(title="每日銷售額") ) bar.render("每日銷售額柱狀圖.html")
可視化
數(shù)據(jù)集
鏈接:https://pan.baidu.com/s/1P3n-gvooVvmHEPak-xmkKg
提取碼:hxvn
到此這篇關(guān)于Python面向?qū)ο髮?shí)現(xiàn)數(shù)據(jù)分析的實(shí)例詳解的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)分析內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)多層感知器MLP(基于雙月數(shù)據(jù)集)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)多層感知器MLP,基于雙月數(shù)據(jù)集,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01vue.js實(shí)現(xiàn)輸入框輸入值內(nèi)容實(shí)時(shí)響應(yīng)變化示例
這篇文章主要介紹了vue.js實(shí)現(xiàn)輸入框輸入值內(nèi)容實(shí)時(shí)響應(yīng)變化,結(jié)合實(shí)例形式分析了vue.js使用v-model屬性進(jìn)行數(shù)據(jù)綁定的相關(guān)操作技巧,需要的朋友可以參考下2018-07-07python 獲取一個(gè)值在某個(gè)區(qū)間的指定倍數(shù)的值方法
今天小編就為大家分享一篇python 獲取一個(gè)值在某個(gè)區(qū)間的指定倍數(shù)的值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11pycharm新建Vue項(xiàng)目的方法步驟(圖文)
這篇文章主要介紹了pycharm新建Vue項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03利用python實(shí)現(xiàn)短信和電話提醒功能的例子
今天小編就為大家分享一篇利用python實(shí)現(xiàn)短信和電話提醒功能的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08簡單幾步教你學(xué)會(huì)Python接口自動(dòng)化測試
這篇文章主要介紹了簡單幾步教你學(xué)會(huì)Python接口自動(dòng)化測試,本文從一個(gè)簡單的登錄接口測試入手,一步步調(diào)整優(yōu)化接口調(diào)用姿勢,期望讀者可以通過本文對(duì)接口自動(dòng)化測試有一個(gè)大致的了解,需要的朋友可以參考下2023-08-08