Pandas常用累計、同比、環(huán)比等統(tǒng)計方法實踐過程
統(tǒng)計表中常常以本年累計、上年同期(累計)、當期(例如當月)完成、上月完成為統(tǒng)計數(shù)據(jù),并進行同比、環(huán)比分析。
如下月報統(tǒng)計表所示樣例,本文將使用Python Pandas工具進行統(tǒng)計。

其中:
- (本年)累計:是指本年1月到截止月份的合計數(shù)
- (上年)同期(累計):是指去年1月到與本年累計所對應截止月份的合計數(shù)
- 同比(增長率)=(本期數(shù)-同期數(shù))/同期數(shù)*100%
- 環(huán)比(增長率)=(本期數(shù)-上期數(shù))/上期數(shù)*100%
注:這里的本期是指本月完成或當月完成,上期數(shù)是指上月完成。
示例數(shù)據(jù):

注:為了演示方便,本案例數(shù)據(jù)源僅使用2年,且每年5個月的數(shù)據(jù)。
1.(本年)累計
在做統(tǒng)計分析開發(fā)中,按年度、按月累計某些統(tǒng)計數(shù)據(jù),是比較常見的需求。對于數(shù)據(jù)來說,就是按規(guī)則逐行累加數(shù)據(jù)。
Pandas中的cumsum()函數(shù)可以實現(xiàn)按某時間維度累計需求。
# 取本年累計值
import pandas as pd
df = pd.read_csv('data2021.csv')
cum_columns_name = ['cum_churncount','cum_newcount']
df[cum_columns_name] = df[['years','churncount','newcount']].groupby(['years']).cumsum()注:其中分組‘years’是指年度時間維度累計。
計算結(jié)果如下:

2.(上年)同期累計
對于(上年)同期累計,將直接取上一年度累計值的同月份數(shù)據(jù)。pandas DataFrame.shift()函數(shù)可以把數(shù)據(jù)移動指定的行數(shù)。

接續(xù)上列,讀取同期數(shù)據(jù)。首先是把‘yearmonth’上移五行,如上圖所示得到新的DataFrame,通過‘yearmonth’進行兩表數(shù)據(jù)關聯(lián)(左關聯(lián):左側(cè)為原表,右側(cè)為移動后的新表),實現(xiàn)去同期數(shù)據(jù)效果。
cum_columns_dict = {'cum_churncount':'cum_same_period_churncount',
'cum_newcount':'cum_same_period_newcount'}
df_cum_same_period = df[['cum_churncount','cum_newcount','yearmonth']].copy()
df_cum_same_period = df_cum_same_period.rename(columns=cum_columns_dict)
#df_cum_same_period.loc[:,'yearmonth'] = df_cum_same_period['yearmonth'].shift(-12) # 一年12個月
df_cum_same_period.loc[:,'yearmonth'] = df_cum_same_period['yearmonth'].shift(-5) # 由于只取5個月數(shù)據(jù)的原因
df = pd.merge(left=df,right=df_cum_same_period,on='yearmonth',how='left')
3. 上月(完成)
取上月的數(shù)據(jù),使用pandas DataFrame.shift()函數(shù)把數(shù)據(jù)移動指定的行數(shù)。
接續(xù)上列,讀取上期數(shù)據(jù)。(與取同期原理一樣,略)
last_mnoth_columns_dict = {'churncount':'last_month_churncount',
'newcount':'last_month_newcount'}
df_last_month = df[['churncount','newcount','yearmonth']].copy()
df_last_month = df_last_month.rename(columns=last_mnoth_columns_dict)
df_last_month.loc[:,'yearmonth'] = df_last_month['yearmonth'].shift(-1) # 移動一行
df = pd.merge(left=df,right=df_last_month,on='yearmonth',how='left')
4. 同比(增長率)
計算同比涉及到除法,需要剔除除數(shù)為零的數(shù)據(jù)。
df.fillna(0,inplace=True) # 空值填充為0 # 計算同比 df.loc[df['cum_same_period_churncount']!=0,'cum_churncount_rat'] = (df['cum_churncount']-df['cum_same_period_churncount'])/df['cum_same_period_churncount'] # 除數(shù)不能為零 df.loc[df['cum_same_period_newcount']!=0,'cum_newcount_rat'] = (df['cum_newcount']-df['cum_same_period_newcount'])/df['cum_same_period_newcount'] # 除數(shù)不能為零 df[['yearmonth','cum_churncount','cum_newcount','cum_same_period_churncount','cum_same_period_newcount','cum_churncount_rat','cum_newcount_rat']]

5. 環(huán)比(增長率)
# 計算環(huán)比 df.loc[df['last_month_churncount']!=0,'churncount_rat'] = (df['churncount']-df['last_month_churncount'])/df['last_month_churncount'] # 除數(shù)不能為零 df.loc[df['last_month_newcount']!=0,'newcount_rat'] = (df['newcount']-df['last_month_newcount'])/df['last_month_newcount'] # 除數(shù)不能為零 df[['yearmonth','churncount','newcount','last_month_churncount','last_month_newcount','churncount_rat','newcount_rat']]

6. 總結(jié)
pandas做統(tǒng)計計算功能方法比較多,這里總結(jié)用到的技術(shù)有累計cumsum()函數(shù)、移動數(shù)據(jù)shift()函數(shù)、表合并關聯(lián)merge()函數(shù),以及通過loc條件修改數(shù)據(jù)。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python GUI庫圖形界面開發(fā)之PyQt5單行文本框控件QLineEdit詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5單行文本框控件QLineEdit詳細使用方法與實例,需要的朋友可以參考下2020-02-02
tensorflow入門:TFRecordDataset變長數(shù)據(jù)的batch讀取詳解
今天小編就為大家分享一篇tensorflow入門:TFRecordDataset變長數(shù)據(jù)的batch讀取詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python實現(xiàn)Excel表格轉(zhuǎn)HTML
Excel工作簿是常用的表格格式,廣泛用于組織、分析及展示數(shù)據(jù),這篇文章主要為大家詳細介紹了如何使用Python將Excel工作簿或工作表轉(zhuǎn)換為HTML文件,需要的可以參考下2024-03-03
Python二進制文件讀取并轉(zhuǎn)換為浮點數(shù)詳解
這篇文章主要介紹了Python二進制文件讀取并轉(zhuǎn)換為浮點數(shù)詳解,用python讀取二進制文件,這里主要用到struct包,而這個包里面的方法主要是unpack、pack、calcsize。,需要的朋友可以參考下2019-06-06

