最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pandas數(shù)據(jù)處理進(jìn)階詳解

 更新時(shí)間:2019年10月11日 08:24:48   作者:Tanyxn  
這篇文章主要介紹了pandas數(shù)據(jù)處理進(jìn)階詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、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())
# 對(duì)于兩列的統(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對(duì)于非數(shù)值型數(shù)據(jù)的統(tǒng)計(jì)分析

(1)對(duì)于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、可以通過(guò) 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)化 --可以通過(guò)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í)間序列的屬性---可以通過(guò)列表推導(dǎo)式來(lái)獲取時(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)
 
# 返回對(duì)象
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ù)班級(jí)分組、統(tǒng)計(jì)學(xué)員的班級(jí)的平均年齡
# 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í)對(duì)age 求平均值、對(duì)userid 求最大值
# 只需要指定 np.方法名
print(users.agg({'age': np.mean, 'USER_ID': np.max}))
 
# 對(duì)age 和 USER_ID 同時(shí)分別求 和 和均值
print(users[['age', 'USER_ID']].agg([np.sum, np.mean]))
 
# 對(duì)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)式來(lái)獲取日屬性
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)所針對(duì)對(duì)象,要關(guān)心的數(shù)據(jù)主體
# index --按照index 進(jìn)行行分組
# columns ---按照columns進(jìn)行列分組
# aggfunc ---對(duì)主體 進(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ì)這兩列的相對(duì)個(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íng)業(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)式來(lái)獲取日屬性
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、求不同門(mén)店的銷售額占比
# 提示:訂單中沒(méi)有銷售額字段,所有需要新增一個(gè)銷售額字段。增加字段后按照門(mén)店編號(hào)進(jìn)行分組,然后計(jì)算占比。
 
# # 先計(jì)算銷售額
# data.loc[:,'銷售額'] = data.loc[:,'單價(jià)'] * data.loc[:,'銷量']
#
# # 按照門(mén)店編號(hào)進(jìn)行分組統(tǒng)計(jì)銷售額的sum
# res = data.groupby(by='門(mén)店編號(hào)')['銷售額'].sum()
# # print(res)
# # 計(jì)算所有的銷售額總和
# all_ = res.sum()
#
# # print(all_)
# per_ = res / all_
#
# print("各個(gè)門(mén)店的銷售額占比為:\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í)間段對(duì)應(yīng)的客流量,但是訂單表中既有日期又有時(shí)間,我們需要從中提出小時(shí)數(shù),這里利用訂單ID去重計(jì)數(shù)代表客流量。
 
# 先對(duì)訂單去重
# subset 去重的那一列 的列名,可以是多列,多列的時(shí)候傳列表
data.drop_duplicates(subset='訂單ID', inplace=True)
 
# print(data.shape)
 
# 按照小時(shí)分組對(duì)訂單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)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 部署django項(xiàng)目安裝uwsgi出錯(cuò)的解決方法總結(jié)

    部署django項(xiàng)目安裝uwsgi出錯(cuò)的解決方法總結(jié)

    uwsgi協(xié)議是一個(gè)uWSGI服務(wù)器自有的協(xié)議,它用于定義傳輸信息的類型(type of information),每一個(gè)uwsgi packet前4byte為傳輸信息類型描述,它與WSGI相比是兩樣?xùn)|西,下面這篇文章主要給大家介紹了關(guān)于部署django項(xiàng)目安裝uwsgi出錯(cuò)的解決方法,需要的朋友可以參考下
    2022-08-08
  • python獲取交互式ssh shell的方法

    python獲取交互式ssh shell的方法

    今天小編就為大家分享一篇python獲取交互式ssh shell的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • django中cookiecutter的使用教程

    django中cookiecutter的使用教程

    這篇文章主要給大家介紹了關(guān)于django中cookiecutter使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Python中的特殊語(yǔ)法:filter、map、reduce、lambda介紹

    Python中的特殊語(yǔ)法:filter、map、reduce、lambda介紹

    這篇文章主要介紹了Python中的特殊語(yǔ)法:filter、map、reduce、lambda介紹,本文分別對(duì)這個(gè)特殊語(yǔ)法給出了代碼實(shí)例,需要的朋友可以參考下
    2015-04-04
  • python jieba分詞并統(tǒng)計(jì)詞頻后輸出結(jié)果到Excel和txt文檔方法

    python jieba分詞并統(tǒng)計(jì)詞頻后輸出結(jié)果到Excel和txt文檔方法

    本篇文章主要介紹了python jieba分詞并統(tǒng)計(jì)詞頻后輸出結(jié)果到Excel和txt文檔方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Python快速?gòu)囊曨l中提取視頻幀的方法詳解

    Python快速?gòu)囊曨l中提取視頻幀的方法詳解

    本文為大家介紹一種從視頻中抽取視頻幀的方法,由于單線程抽取視頻幀速度較慢,因此這里我們?cè)黾恿硕嗑€程的方法,感興趣的小伙伴可以動(dòng)手嘗試一下
    2022-07-07
  • Python采集電影評(píng)論實(shí)戰(zhàn)示例

    Python采集電影評(píng)論實(shí)戰(zhàn)示例

    這篇文章主要為大家介紹了Python采集電影評(píng)論實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • python 消除 futureWarning問(wèn)題的解決

    python 消除 futureWarning問(wèn)題的解決

    今天小編就為大家分享一篇python 消除 futureWarning問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • 在Python中操作字典之update()方法的使用

    在Python中操作字典之update()方法的使用

    這篇文章主要介紹了在Python中操作字典之update()方法的使用,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • ZABBIX3.2使用python腳本實(shí)現(xiàn)監(jiān)控報(bào)表的方法

    ZABBIX3.2使用python腳本實(shí)現(xiàn)監(jiān)控報(bào)表的方法

    今天小編就為大家分享一篇ZABBIX3.2使用python腳本實(shí)現(xiàn)監(jiān)控報(bào)表的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07

最新評(píng)論

台江县| 普安县| 维西| 柏乡县| 云阳县| 乌兰浩特市| 巴彦淖尔市| 泰和县| 崇州市| 龙口市| 沙河市| 开封县| 嘉善县| 阿图什市| 南昌县| 大田县| 翼城县| 云浮市| 通山县| 神池县| 微山县| 五寨县| 通河县| 扶绥县| 运城市| 兴城市| 大石桥市| 南丹县| 酉阳| 纳雍县| 丁青县| 苏尼特左旗| 卢氏县| 大竹县| 石景山区| 务川| 乌什县| 南平市| 丰原市| 锦州市| 锦屏县|