99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)大數(shù)據(jù)時(shí)代數(shù)十萬(wàn)數(shù)據(jù)Excel數(shù)據(jù)不好處理怎么辦?
數(shù)十萬(wàn)數(shù)據(jù)Excel數(shù)據(jù)不好處理怎么辦?
2020-07-16
收藏

文章轉(zhuǎn)載自:微信公眾號(hào) Python的樂趣

作者:一粒米飯

比如像電商行業(yè),每月有上百萬(wàn)條訂單發(fā)貨數(shù)據(jù)需要與倉(cāng)庫(kù)的數(shù)據(jù)進(jìn)行核對(duì)計(jì)算,涉及到數(shù)據(jù)計(jì)算,篩選,匹配等步驟,用excel表超級(jí)卡,并且經(jīng)??ㄋ?。

這時(shí)如果你會(huì)Python,十幾行代碼就可以搞定。

這里需要兩個(gè)Python庫(kù),一個(gè)是os庫(kù),一個(gè)是pandas庫(kù)。

os庫(kù)

os是Python內(nèi)置庫(kù),不需要額外安裝,只要用import導(dǎo)入就可以用了。os模塊封裝了常見的文件和目錄操作,利用它可以輕松的對(duì)系統(tǒng)上的目錄和文件進(jìn)行各種操作,比如獲取當(dāng)前目錄、列舉當(dāng)前文件夾中的所有文件和文件夾、判斷文件或目錄是否存在、刪除文件等,具體見下圖。

pandas庫(kù)

pandas是第三方庫(kù),需要手動(dòng)安裝才能使用。pandas是專門用來(lái)做數(shù)據(jù)分析的強(qiáng)大類庫(kù),可以方便地從csv、Excel和其他文本文件以及數(shù)據(jù)庫(kù)中讀取數(shù)據(jù),然后對(duì)數(shù)據(jù)進(jìn)行加和、求平均值、求方差、計(jì)算最大值最小值等數(shù)據(jù)分析,支持生成Excel等格式文件或進(jìn)行可視化操作,函數(shù)如下:

其中讀Excel需要依賴xlrd庫(kù),寫Excel依賴openpyxl,pandas、xlrd和openpyxl安裝命令如下:

$ pip install xlrd openpyxl pandas

下面開始進(jìn)行數(shù)據(jù)處理...

這里假設(shè)數(shù)據(jù)是按日期命名的Excel文件并且放在excel_data文件夾中,每個(gè)Excel文件包含用戶ID、商品ID、商品屬性列表、購(gòu)買數(shù)量這幾列信息。

文件夾中的所有文件如下,在linux下用ls命令列舉excel_data下所有文件:

$ ls excel_data

結(jié)果:

20120702.xlsx 20131018.xlsx 20150203.xlsx 20170416.xlsx
20120703.xlsx 20131019.xlsx 20150204.xlsx 20170417.xlsx
20120704.xlsx 20131020.xlsx 20150205.xlsx 20170418.xlsx
20120705.xlsx 20131021.xlsx 20160101.xlsx 20170419.xlsx
...

實(shí)現(xiàn)的思路是利用os庫(kù)獲取所有的Excel文件,然后用pandas依次讀取所有文件并合并到一起進(jìn)行數(shù)據(jù),計(jì)算出每個(gè)商品的總量以及銷量前十的商品。

1.列舉所有Excel文件

import os
files = os.listdir("excel_data")

2.用pandas讀取所有數(shù)據(jù)并合并到一起

import pandas as pd
df_list = [pd.read_excel(os.path.join("excel_data", f)) for f in files]
data = pd.concat(df_list)

3.統(tǒng)計(jì)每個(gè)商品的數(shù)量

sum_of_product = data[["商品ID", "購(gòu)買數(shù)量"]].groupby(["商品ID"]).sum()
sum_of_product

結(jié)果

購(gòu)買數(shù)量
商品ID	
1662	1
201826	17
203319	67
203320	494
203322	332
...	...

122680025	21
122680026	8
122690023	16
122692024	48
122696024	5

獲取銷量前十的商品

sum_of_product.sort_values('購(gòu)買數(shù)量', ascending=False).head(10)

結(jié)果:

商品ID	      購(gòu)買數(shù)量
50018831	56632
50007016	8291
50011993	6351
50013636	6340
50003700	6325
211122	5823
50010558	5248
50016006	4948
50006602	4692
50002524	4123

完整代碼如下:

import os
import pandas as pd

# 獲取所有Excel文件并讀取數(shù)據(jù)
files = os.listdir("excel_data")
df_list = [pd.read_excel(os.path.join("excel_data", f)) for f in files]
data = pd.concat(df_list)

# 統(tǒng)計(jì)每個(gè)商品的數(shù)量,并輸出到Excel文件中
sum_of_product = data[["商品ID", "購(gòu)買數(shù)量"]].groupby(["商品ID"]).sum()
sum_of_product.to_excel("各個(gè)商品數(shù)量統(tǒng)計(jì).xlsx")

# 統(tǒng)計(jì)銷量前十的商品
sum_of_product.sort_values('購(gòu)買數(shù)量', ascending=False).head(10)

結(jié)果:

商品ID	購(gòu)買數(shù)量
50018831	56632
50007016	8291
50011993	6351
50013636	6340
50003700	6325
211122	5823
50010558	5248
50016006	4948
50006602	4692
50002524	4123

教程就到這里,不足之處歡迎交流指正

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }