pandas數(shù)據(jù)處理進(jìn)階詳解
一、pandas的統(tǒng)計(jì)分析
1、關(guān)于pandas 的數(shù)值統(tǒng)計(jì)(統(tǒng)計(jì)detail 中的 單價(jià)的相關(guān)指標(biāo))
import pandas as pd # 加載數(shù)據(jù) detail = pd.read_excel("./meal_order_detail.xlsx") print("detail :\n", detail) print("detail 的列索引名稱:\n", detail.columns) print("detail 的形狀:\n", detail.shape) print("detail 數(shù)據(jù)類型:\n", detail.dtypes) print("amounts 的最大值:\n",detail.loc[:,'amounts'].max()) print("amounts 的最小值:\n",detail.loc[:,'amounts'].min()) print("amounts 的均值:\n",detail.loc[:,'amounts'].mean()) print("amounts 的中位數(shù):\n",detail.loc[:,'amounts'].median()) print("amounts 的方差:\n",detail.loc[:,'amounts'].var()) print("amounts 的describe:\n",detail.loc[:,'amounts'].describe()) # 對于兩列的統(tǒng)計(jì)結(jié)果 print("amounts 的describe:\n",detail.loc[:,['counts','amounts']].describe()) print("amounts 的describe:\n",detail.loc[:,['counts','amounts']].describe()) print("amounts 的describe:\n",detail.loc[:,'amounts'].describe()) print("amounts 的describe:\n",detail.loc[:,'counts'].describe()) print("amounts 的極差:\n",detail.loc[:,'amounts'].ptp()) print("amounts 的標(biāo)準(zhǔn)差:\n",detail.loc[:,'amounts'].std()) print("amounts 的眾數(shù):\n",detail.loc[:,'amounts'].mode()) # 返回眾數(shù)的數(shù)組 print("amounts 的眾數(shù):\n",detail.loc[:,'counts'].mode()) # 返回眾數(shù)的數(shù)組 print("amounts 的非空值的數(shù)目:\n",detail.loc[:,'amounts'].count()) print("amounts 的最大值的位置:\n",detail.loc[:,'amounts'].idxmax()) # np.argmax() print("amounts 的最小值的位置:\n",detail.loc[:,'amounts'].idxmin()) # np.argmin()
2、pandas對于非數(shù)值型數(shù)據(jù)的統(tǒng)計(jì)分析
(1)對于dataframe轉(zhuǎn)化數(shù)據(jù)類型,其他類型 轉(zhuǎn)化為object類型
detail.loc[:,'amounts'] = detail.loc[:,'amounts'].astype('object')
(2)類別型數(shù)據(jù)
detail.loc[:,'amounts'] = detail.loc[:,'amounts'].astype('category') print("統(tǒng)計(jì)類別型數(shù)據(jù)的describe指標(biāo):\n",detail.loc[:, 'amounts'].describe())
(3)統(tǒng)計(jì)實(shí)例
## 在detail中 哪些菜品最火?菜品賣出了多少份? # 若白飯算菜 detail.loc[:, 'dishes_name'] = detail.loc[:, 'dishes_name'].astype('category') print("按照dishes_name統(tǒng)計(jì)描述信息:\n", detail.loc[:, 'dishes_name'].describe()) # 若白飯不算菜 ---把白飯刪除,再統(tǒng)計(jì) # drop labels ---行的名稱, axis =0,inplace = True # 行的名稱??? 怎么獲取----bool值 # 定位到白飯的行 bool_id = detail.loc[:, 'dishes_name'] == '白飯/大碗' # 進(jìn)行 獲取行名稱 index = detail.loc[bool_id, :].index # 進(jìn)行刪除 detail.drop(labels=index, axis=0, inplace=True) # 在進(jìn)行轉(zhuǎn)化類型 detail.loc[:, 'dishes_name'] = detail.loc[:, 'dishes_name'].astype('category') # 在進(jìn)行統(tǒng)計(jì)描述信息 print("按照dishes_name統(tǒng)計(jì)描述信息:\n", detail.loc[:, 'dishes_name'].describe()) # 看 在detail 中那個(gè)訂單點(diǎn)的菜最多,點(diǎn)了多少份菜? # 將 order_id 轉(zhuǎn)化為類別型數(shù)據(jù) ,再 進(jìn)行describe detail.loc[:, 'order_id'] = detail.loc[:, 'order_id'].astype("category") # 統(tǒng)計(jì)描述 print("按照order_id統(tǒng)計(jì)描述信息為:\n", detail.loc[:, 'order_id'].describe())
二、pandas時(shí)間數(shù)據(jù)
- datetime64[ns] ---numpy 里面的時(shí)間點(diǎn)類
- Timestamp ---pandas 默認(rèn)的時(shí)間點(diǎn)類型----封裝了datetime64[ns]
- DatetimeIndex ---pandas 默認(rèn)支持的時(shí)間序列結(jié)構(gòu)
1、可以通過 pd.to_datetime 將時(shí)間點(diǎn)數(shù)據(jù)轉(zhuǎn)化為pandas默認(rèn)支持的時(shí)間點(diǎn)數(shù)據(jù)
res = pd.to_datetime("2016/01/01") print("res:\n",res) print("res 的類型:\n",type(res))
2、時(shí)間序列轉(zhuǎn)化 --可以通過pd.to_datetime 或者pd.DatetimeIndex將時(shí)間序列轉(zhuǎn)化為pandas默認(rèn)支持的時(shí)間序列結(jié)構(gòu)
res = pd.to_datetime(['2016-01-01', '2016-01-01', '2016-01-01', '2011-01-01']) res1 = pd.DatetimeIndex(['2016-01-01', '2016-01-02', '2016-02-05', '2011-09-01']) print("res:\n", res) print("res 的類型:\n", type(res)) print("res1:\n", res1) print("res1 的類型:\n", type(res1))
3、
import pandas as pd # #加載數(shù)據(jù) detail = pd.read_excel("./meal_order_detail.xlsx") # print("detail :\n",detail) print("detail 的列索引名稱:\n", detail.columns) print("detail 的形狀:\n", detail.shape) # print("detail 數(shù)據(jù)類型:\n",detail.dtypes) print("*" * 80) # 獲取place_order_time列 print(detail.loc[:, 'place_order_time']) # 轉(zhuǎn)化為pandas默認(rèn)支持的時(shí)間序列結(jié)構(gòu) detail.loc[:, 'place_order_time'] = pd.to_datetime(detail.loc[:, 'place_order_time']) # print(detail.dtypes) print("*" * 80) # 獲取該時(shí)間序列的屬性---可以通過列表推導(dǎo)式來獲取時(shí)間點(diǎn)的屬性 year = [i.year for i in detail.loc[:, 'place_order_time']] print("年:\n", year) month = [i.month for i in detail.loc[:, 'place_order_time']] print("月:\n", month) day = [i.day for i in detail.loc[:, 'place_order_time']] print("日:\n", day) quarter = [i.quarter for i in detail.loc[:, 'place_order_time']] print("季度:\n", quarter) # 返回對象 weekday = [i.weekday for i in detail.loc[:, 'place_order_time']] print("周幾:\n", weekday) weekday_name = [i.weekday_name for i in detail.loc[:, 'place_order_time']] print("周幾:\n", weekday_name) is_leap_year = [i.is_leap_year for i in detail.loc[:, 'place_order_time']] print("是否閏年:\n", is_leap_year)
4、時(shí)間加減
import pandas as pd res = pd.to_datetime("2016-01-01") print("res:\n", res) print("res 的類型:\n", type(res)) print("時(shí)間推后一天:\n", res + pd.Timedelta(days=1)) print("時(shí)間推后一小時(shí):\n", res + pd.Timedelta(hours=1)) detail.loc[:, 'place_over_time'] = detail.loc[:, 'place_order_time'] + pd.Timedelta(days=1) print(detail) ## 時(shí)間差距計(jì)算 res = pd.to_datetime('2019-10-9') - pd.to_datetime('1996-11-07') print(res)
5、獲取本機(jī)可以使用的最初時(shí)間 和最后使用的時(shí)間節(jié)點(diǎn)
print(pd.Timestamp.min) print(pd.Timestamp.max)
三、分組聚合
import pandas as pd import numpy as np # 加載數(shù)據(jù) users = pd.read_excel("./users.xlsx") print("users:\n", users) print("users 的列索引:\n", users.columns) print("users 的數(shù)據(jù)類型:\n", users.dtypes) # 根據(jù)班級分組、統(tǒng)計(jì)學(xué)員的班級的平均年齡 # groupby 分組 # by ---指定分組的列,可以是單列 也可以是多列 # res = users.groupby(by='ORGANIZE_NAME')['age'].mean() # 按照單列進(jìn)行分組,統(tǒng)計(jì)多個(gè)列的指標(biāo) # res = users.groupby(by='ORGANIZE_NAME')[['age','USER_ID']].mean() res = users.groupby(by=['ORGANIZE_NAME', 'poo', 'sex'])['age'].mean() print(res) # 利用agg # 進(jìn)行同時(shí)對age 求平均值、對userid 求最大值 # 只需要指定 np.方法名 print(users.agg({'age': np.mean, 'USER_ID': np.max})) # 對age 和 USER_ID 同時(shí)分別求 和 和均值 print(users[['age', 'USER_ID']].agg([np.sum, np.mean])) # 對age USER_ID 求取不同個(gè)數(shù)的統(tǒng)計(jì)指標(biāo) print(users.agg({'age': np.min, 'USER_ID': [np.mean, np.sum]})) def hh(x): return x + 1 # 自定義函數(shù)進(jìn)行計(jì)算 # res = users['age'].apply(hh) # res = users[['age','USER_ID']].apply(lambda x:x+1) res = users['age'].transform(lambda x: x + 1) # 不能進(jìn)行跨列的運(yùn)算 print(res)
四、透視表與交叉表
import pandas as pd # 加載數(shù)據(jù) detail = pd.read_excel("./meal_order_detail.xlsx") print("detail :\n", detail) print("detail 的列名:\n", detail.columns) print("detail 的數(shù)據(jù)類型:\n", detail.dtypes) # 獲取時(shí)間點(diǎn)的日屬性 # 必須pandas默認(rèn)支持的時(shí)間序列類型 detail.loc[:, 'place_order_time'] = pd.to_datetime(detail.loc[:, 'place_order_time']) # 以列表推導(dǎo)式來獲取日屬性 detail.loc[:, 'day'] = [i.day for i in detail.loc[:, 'place_order_time']] # 透視表 是一種plus 版的分組聚合 # 創(chuàng)建一個(gè)透視表 # data dataframe數(shù)據(jù) # values 最終統(tǒng)計(jì)指標(biāo)所針對對象,要關(guān)心的數(shù)據(jù)主體 # index --按照index 進(jìn)行行分組 # columns ---按照columns進(jìn)行列分組 # aggfunc ---對主體 進(jìn)行什么指標(biāo)的統(tǒng)計(jì) # res = pd.pivot_table(data=detail[['amounts','order_id','counts','dishes_name','day']],values='amounts',columns=['day','counts'],index=['order_id','dishes_name'],aggfunc='mean',margins=True) # # print(res) # res.to_excel("./hh.xlsx") # 交叉表 mini版的透視表 # 如果只傳index 與columns 統(tǒng)計(jì)這兩列的相對個(gè)數(shù) # res = pd.crosstab(index=detail['counts'],columns=detail['amounts']) # values 必須和aggfunc同時(shí)存在 res = pd.crosstab(index=detail['order_id'],columns=detail['counts'],values=detail['amounts'],aggfunc='mean') print(res)
五、案例
1、營業(yè)額案例
import pandas as pd # detail 有時(shí)間數(shù)據(jù) # 加載數(shù)據(jù) detail = pd.read_excel("./meal_order_detail.xlsx") print("detail :\n", detail) print("detail 的列名:\n", detail.columns) print("detail 的數(shù)據(jù)類型:\n", detail.dtypes) # 計(jì)算每個(gè)菜品的銷售額 ,增加到detail detail.loc[:, 'pay'] = detail.loc[:, 'counts'] * detail.loc[:, 'amounts'] # print(detail) # 獲取時(shí)間點(diǎn)的日屬性 # 必須pandas默認(rèn)支持的時(shí)間序列類型 detail.loc[:, 'place_order_time'] = pd.to_datetime(detail.loc[:, 'place_order_time']) # 以列表推導(dǎo)式來獲取日屬性 detail.loc[:, 'day'] = [i.day for i in detail.loc[:, 'place_order_time']] # print(detail) # 以 日 為分組 ,統(tǒng)計(jì)pay的sum res = detail.groupby(by='day')['pay'].sum() print(res) # print(type(res)) df = pd.DataFrame(res.values, columns=['monty'], index=res.index) print(df) print(type(df))
2、連鎖超市案例
import pandas as pd # 加載數(shù)據(jù) order = pd.read_csv("./order.csv", encoding='ansi') print("order:\n", order) print("order 的列索引:\n", order.columns) # 1、哪些類別的商品比較暢銷? # 剔除銷量 < 0 的數(shù)據(jù) (保留銷量 >0 的數(shù)據(jù)) # 保存 bool_id = order.loc[:, '銷量'] > 0 data = order.loc[bool_id, :] # 剔除異常數(shù)據(jù)之后的正常數(shù)據(jù) print(data.shape) print("*" * 80) # 刪除異常 # bool_id = order.loc[:,'銷量'] <= 0 # index = order.loc[bool_id,:].index # # data = order.drop(labels=index,axis=0,inplace=False) # 按照類別進(jìn)行分組,統(tǒng)計(jì)銷量的 和 # 進(jìn)行dataframe或者series的值排序 # 如果series sort_values()直接按照seies的值進(jìn)行排序 # 如果df 那么需要指定 按照哪一列進(jìn)行排序,by= 列名 # 默認(rèn)是升序ascending=True # ascending=False 降序 # res = data.groupby(by='類別ID')['銷量'].sum().sort_values(ascending=False) # # print(res) # 2、哪些商品比較暢銷? # 分組聚合實(shí)現(xiàn) # res = data.groupby(by='商品ID')['銷量'].sum().sort_values(ascending=False).head(10) # # print(res) # 透視表實(shí)現(xiàn) # res = pd.pivot_table(data=data.loc[:, ['商品ID', '銷量']], index='商品ID', values='銷量', aggfunc='sum').sort_values(by='銷量', # ascending=False).head( # 10) # print(res) # 3、求不同門店的銷售額占比 # 提示:訂單中沒有銷售額字段,所有需要新增一個(gè)銷售額字段。增加字段后按照門店編號進(jìn)行分組,然后計(jì)算占比。 # # 先計(jì)算銷售額 # data.loc[:,'銷售額'] = data.loc[:,'單價(jià)'] * data.loc[:,'銷量'] # # # 按照門店編號進(jìn)行分組統(tǒng)計(jì)銷售額的sum # res = data.groupby(by='門店編號')['銷售額'].sum() # # print(res) # # 計(jì)算所有的銷售額總和 # all_ = res.sum() # # # print(all_) # per_ = res / all_ # # print("各個(gè)門店的銷售額占比為:\n",per_.apply(lambda x:format(x,".2%"))) # a = 100.105 # print("%.2f"%a) # print("{}%".format(2.0)) # 匿名函數(shù) # print(lambda x:x+5) # # # def add(x): # # return x+5 # 4、哪段時(shí)間段是超市的客流高峰期? # 提示:需要知道每個(gè)時(shí)間段對應(yīng)的客流量,但是訂單表中既有日期又有時(shí)間,我們需要從中提出小時(shí)數(shù),這里利用訂單ID去重計(jì)數(shù)代表客流量。 # 先對訂單去重 # subset 去重的那一列 的列名,可以是多列,多列的時(shí)候傳列表 data.drop_duplicates(subset='訂單ID', inplace=True) # print(data.shape) # 按照小時(shí)分組對訂單ID進(jìn)行統(tǒng)計(jì)數(shù)量 # 將成交時(shí)間轉(zhuǎn)化為 pandas默認(rèn)支持的時(shí)間序列類型 data.loc[:, '成交時(shí)間'] = pd.to_datetime(data.loc[:, '成交時(shí)間']) # 獲取小時(shí)屬性,增加到data 中 data.loc[:, 'hour'] = [i.hour for i in data.loc[:, '成交時(shí)間']] # print(data) # 按照hour 分組 統(tǒng)計(jì) 訂單ID數(shù)量 res = data.groupby(by='hour')['訂單ID'].count().sort_values(ascending=False) print(res)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python selenium 彈出框處理的實(shí)現(xiàn)
這篇文章主要介紹了python selenium 彈出框處理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02python使用opencv在Windows下調(diào)用攝像頭實(shí)現(xiàn)解析
這篇文章主要介紹了python使用opencv在Windows下調(diào)用攝像頭實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Python爬蟲beautifulsoup4常用的解析方法總結(jié)
今天小編就為大家分享一篇關(guān)于Python爬蟲beautifulsoup4常用的解析方法總結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02python hbase讀取數(shù)據(jù)發(fā)送kafka的方法
今天小編就為大家分享一篇python hbase讀取數(shù)據(jù)發(fā)送kafka的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12用Python實(shí)現(xiàn)定時(shí)備份Mongodb數(shù)據(jù)并上傳到FTP服務(wù)器
這篇文章主要介紹了用Python實(shí)現(xiàn)定時(shí)備份Mongodb數(shù)據(jù)并上傳到FTP服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01