溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Pandas中怎么按日期篩選、顯示及統(tǒng)計數(shù)據(jù)

發(fā)布時間:2021-11-30 15:32:52 來源:億速云 閱讀:570 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下Pandas中怎么按日期篩選、顯示及統(tǒng)計數(shù)據(jù),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

運行環(huán)境為 windows系統(tǒng),64位,python3.5。

1 讀取并整理數(shù)據(jù)

  • 首先引入pandas

1.         import pandas as pd

  • csv文件中讀取數(shù)據(jù)

1.         df = pd.read_csv('date.csv', header=None)

2.         print(df.head(2))

1.                     0  1

2.         0  2013-10-24  3

3.         1  2013-10-25  4

  • 整理數(shù)據(jù)

1.         df.columns = ['date','number']

2.         df['date'] = pd.to_datetime(df['date']) #將數(shù)據(jù)類型轉(zhuǎn)換為日期類型

3.         df = df.set_index('date') # date設(shè)置為index

4.         print(df.head(2))

5.         print(df.tail(2))

6.         print(df.shape)

1.                     number

2.         date              

3.         2013-10-24       3

4.         2013-10-25       4

5.                     number

6.         date              

7.         2017-02-14       6

8.         2017-02-22       6

9.         (425, 1)

  • df的行數(shù)一共是425行。

查看Dataframe的數(shù)據(jù)類型

1.         print(type(df))

2.         print(df.index)

3.         print(type(df.index))

1.         <class 'pandas.core.frame.DataFrame'>

2.         DatetimeIndex(['2013-10-24', '2013-10-25', '2013-10-29', '2013-10-30',

3.                        '2013-11-04', '2013-11-06', '2013-11-08', '2013-11-12',

4.                        '2013-11-14', '2013-11-25',

5.                        ...

6.                        '2017-01-03', '2017-01-07', '2017-01-14', '2017-01-17',

7.                        '2017-01-23', '2017-01-25', '2017-01-26', '2017-02-07',

8.                        '2017-02-14', '2017-02-22'],

9.                       dtype='datetime64[ns]', name='date', length=425, freq=None)

10.      <class 'pandas.tseries.index.DatetimeIndex'>

構(gòu)造Series類型數(shù)據(jù)

1.         s = pd.Series(df['number'], index=df.index)

2.         print(type(s))

3.         s.head(2)

1.         <class 'pandas.core.series.Series'>

2.          

3.         date

4.         2013-10-24    3

5.         2013-10-25    4

6.         Name: number, dtype: int64

2 按日期篩選數(shù)據(jù)

按年度獲取數(shù)據(jù)

1.         print('---------獲取2013年的數(shù)據(jù)-----------')

2.         print(df['2013'].head(2)) # 獲取2013年的數(shù)據(jù)

3.         print(df['2013'].tail(2)) # 獲取2013年的數(shù)據(jù)

1.         ---------獲取2013年的數(shù)據(jù)-----------

2.                     number

3.         date              

4.         2013-10-24       3

5.         2013-10-25       4

6.                     number

7.         date              

8.         2013-12-27       2

9.         2013-12-30       2

獲取20162017年的數(shù)據(jù)

1.         print('---------獲取20162017年的數(shù)據(jù)-----------')

2.         print(df['2016':'2017'].head(2))  #獲取20162017年的數(shù)據(jù)

3.         print(df['2016':'2017'].tail(2))  #獲取20162017年的數(shù)據(jù)

1.         ---------獲取20162017年的數(shù)據(jù)-----------

2.                     number

3.         date              

4.         2016-01-04       4

5.         2016-01-07       6

6.                     number

7.         date              

8.         2017-02-14       6

9.         2017-02-22       6

獲取某月的數(shù)據(jù)

1.         print('---------獲取某月的數(shù)據(jù)-----------')

2.         print(df['2013-11']) # 獲取某月的數(shù)據(jù)

1.         ---------獲取某月的數(shù)據(jù)-----------

2.                     number

3.         date              

4.         2013-11-04       1

5.         2013-11-06       3

6.         2013-11-08       1

7.         2013-11-12       5

8.         2013-11-14       2

9.         2013-11-25       1

10.      2013-11-29       1

獲取具體某天的數(shù)據(jù)

  • 請注意dataframe類型的數(shù)據(jù),獲取具體某天的數(shù)據(jù)時,跟series是有些差異的,詳細(xì)情況如下述代碼所示:

1.         # 按日期篩選數(shù)據(jù)

2.         print('---------獲取具體某天的數(shù)據(jù)-----------')

3.         # 獲取具體某天的數(shù)據(jù)

4.         print(s['2013-11-06'])

5.          

6.         # 獲取具體某天的數(shù)據(jù),用datafrme直接選取某天時會報錯,而series的數(shù)據(jù)就沒有問題

7.         # print(df['2013-11-06'])

8.          

9.         #可以考慮用區(qū)間來獲取某天的數(shù)據(jù)

10.      print(df['2013-11-06':'2013-11-06'])

1.         ---------獲取具體某天的數(shù)據(jù)-----------

2.         3

3.                     number

4.         date              

5.         2013-11-06       3

  • dataframetruncate函數(shù)可以獲取某個時期之前或之后的數(shù)據(jù),或者某個時間區(qū)間的數(shù)據(jù)

  • 但一般建議直接用切片(slice),這樣更為直觀,方便

1.         # dataframetruncate函數(shù)可以獲取某個時期之前或之后的數(shù)據(jù),或者某個時間區(qū)間的數(shù)據(jù)

2.         # 但一般建議直接用切片(slice),這樣更為直觀,方便

3.         print('---------獲取某個時期之前或之后的數(shù)據(jù)-----------')

4.         print('--------after------------')

5.         print(df.truncate(after = '2013-11'))

6.         print('--------before------------')

7.         print(df.truncate(before='2017-02'))

1.         ---------獲取某個時期之前或之后的數(shù)據(jù)-----------

2.         --------after------------

3.                     number

4.         date              

5.         2013-10-24       3

6.         2013-10-25       4

7.         2013-10-29       2

8.         2013-10-30       1

9.         --------before------------

10.                  number

11.      date              

12.      2017-02-07       8

13.      2017-02-14       6

14.      2017-02-22       6

3 按日期顯示數(shù)據(jù)

3.1 to_period()方法

  • 請注意df.index的數(shù)據(jù)類型是DatetimeIndex

  • df_peirod的數(shù)據(jù)類型是PeriodIndex

按月顯示,但不統(tǒng)計

1.         df_period = df.to_period('M') #按月顯示,但不統(tǒng)計

2.         print(type(df_period))

3.          

4.         print(type(df_period.index))

5.         # 請注意df.index的數(shù)據(jù)類型是DatetimeIndex;

6.         # df_peirod的數(shù)據(jù)類型是PeriodIndex

7.          

8.         print(df_period.head())

1.         <class 'pandas.core.frame.DataFrame'>

2.         <class 'pandas.tseries.period.PeriodIndex'>

3.                  number

4.         date          

5.         2013-10       3

6.         2013-10       4

7.         2013-10       2

8.         2013-10       1

9.         2013-11       1

按季度顯示,但不統(tǒng)計

1.         print(df.to_period('Q').head()) #按季度顯示,但不統(tǒng)計

1.                 number

2.         date          

3.         2013Q4       3

4.         2013Q4       4

5.         2013Q4       2

6.         2013Q4       1

7.         2013Q4       1

按年度顯示,但不統(tǒng)計

1.         print(df.to_period('A').head()) #按年度顯示,但不統(tǒng)計

1.               number

2.         date        

3.         2013       3

4.         2013       4

5.         2013       2

6.         2013       1

7.         2013       1

3.2 asfreq()方法

按年度頻率顯示

1.         df_period.index.asfreq('A') # 'A'默認(rèn)是'A-DEC',其他如'A-JAN'

1.         PeriodIndex(['2013', '2013', '2013', '2013', '2013', '2013', '2013', '2013',

2.                      '2013', '2013',

3.                      ...

4.                      '2017', '2017', '2017', '2017', '2017', '2017', '2017', '2017',

5.                      '2017', '2017'],

6.                     dtype='period[A-DEC]', name='date', length=425, freq='A-DEC')

1.         df_period.index.asfreq('A-JAN') # 'A'默認(rèn)是'A-DEC',其他如'A-JAN'

1.         PeriodIndex(['2014', '2014', '2014', '2014', '2014', '2014', '2014', '2014',

2.                      '2014', '2014',

3.                      ...

4.                      '2017', '2017', '2017', '2017', '2017', '2017', '2017', '2018',

5.                      '2018', '2018'],

6.                     dtype='period[A-JAN]', name='date', length=425, freq='A-JAN')

  • 按年度頻率在不同情形下的顯示,可參考下圖所示:

按季度頻率顯示

1.         df_period.index.asfreq('Q') # 'Q'默認(rèn)是'Q-DEC',其他如“Q-SEP”,“Q-FEB”

1.         PeriodIndex(['2013Q4', '2013Q4', '2013Q4', '2013Q4', '2013Q4', '2013Q4',

2.                      '2013Q4', '2013Q4', '2013Q4', '2013Q4',

3.                      ...

4.                      '2017Q1', '2017Q1', '2017Q1', '2017Q1', '2017Q1', '2017Q1',

5.                      '2017Q1', '2017Q1', '2017Q1', '2017Q1'],

6.                     dtype='period[Q-DEC]', name='date', length=425, freq='Q-DEC')

1.         df_period.index.asfreq('Q-SEP') # 可以顯示不同的季度財年,“Q-SEP”,“Q-FEB”

2.         # df_period.index = df_period.index.asfreq('Q-DEC') # 可以顯示不同的季度財年,“Q-SEP”,“Q-FEB”

3.         # print(df_period.head())

1.         PeriodIndex(['2014Q1', '2014Q1', '2014Q1', '2014Q1', '2014Q1', '2014Q1',

2.                      '2014Q1', '2014Q1', '2014Q1', '2014Q1',

3.                      ...

4.                      '2017Q2', '2017Q2', '2017Q2', '2017Q2', '2017Q2', '2017Q2',

5.                      '2017Q2', '2017Q2', '2017Q2', '2017Q2'],

6.                     dtype='period[Q-SEP]', name='date', length=425, freq='Q-SEP')

  • 按季度頻率在不同情形下的顯示,可參考下圖所示:

按月度頻率顯示

1.         df_period.index.asfreq('M') # 按月份顯示

1.         PeriodIndex(['2013-10', '2013-10', '2013-10', '2013-10', '2013-11', '2013-11',

2.                      '2013-11', '2013-11', '2013-11', '2013-11',

3.                      ...

4.                      '2017-01', '2017-01', '2017-01', '2017-01', '2017-01', '2017-01',

5.                      '2017-01', '2017-02', '2017-02', '2017-02'],

6.                     dtype='period[M]', name='date', length=425, freq='M')

按工作日顯示

  • method     1

1.         df_period.index.asfreq('B', how='start') # 按工作日期顯示

1.         PeriodIndex(['2013-10-01', '2013-10-01', '2013-10-01', '2013-10-01',

2.                      '2013-11-01', '2013-11-01', '2013-11-01', '2013-11-01',

3.                      '2013-11-01', '2013-11-01',

4.                      ...

5.                      '2017-01-02', '2017-01-02', '2017-01-02', '2017-01-02',

6.                      '2017-01-02', '2017-01-02', '2017-01-02', '2017-02-01',

7.                      '2017-02-01', '2017-02-01'],

8.                     dtype='period[B]', name='date', length=425, freq='B')

  • method     2

1.         df_period.index.asfreq('B', how='end') # 按工作日期顯示

1.         PeriodIndex(['2013-10-31', '2013-10-31', '2013-10-31', '2013-10-31',

2.                      '2013-11-29', '2013-11-29', '2013-11-29', '2013-11-29',

3.                      '2013-11-29', '2013-11-29',

4.                      ...

5.                      '2017-01-31', '2017-01-31', '2017-01-31', '2017-01-31',

6.                      '2017-01-31', '2017-01-31', '2017-01-31', '2017-02-28',

7.                      '2017-02-28', '2017-02-28'],

8.                     dtype='period[B]', name='date', length=425, freq='B')

4 按日期統(tǒng)計數(shù)據(jù)

4.1按日期統(tǒng)計數(shù)據(jù)

按周統(tǒng)計數(shù)據(jù)

1.         print(df.resample('w').sum().head())

2.         # “w”,week

1.                     number

2.         date              

3.         2013-10-27     7.0

4.         2013-11-03     3.0

5.         2013-11-10     5.0

6.         2013-11-17     7.0

7.         2013-11-24     NaN

按月統(tǒng)計數(shù)據(jù)

1.         print(df.resample('M').sum().head())

2.         # "MS"是每個月第一天為開始日期, "M"是每個月最后一天

1.                     number

2.         date              

3.         2013-10-31      10

4.         2013-11-30      14

5.         2013-12-31      27

6.         2014-01-31      16

7.         2014-02-28       4

按季度統(tǒng)計數(shù)據(jù)

1.         print(df.resample('Q').sum().head())

2.         # "QS"是每個季度第一天為開始日期, "Q"是每個季度最后一天

1.                     number

2.         date              

3.         2013-12-31      51

4.         2014-03-31      73

5.         2014-06-30      96

6.         2014-09-30     136

7.         2014-12-31     148

按年統(tǒng)計數(shù)據(jù)

1.         print(df.resample('AS').sum())

2.         # "AS"是每年第一天為開始日期, "A是每年最后一天

1.                     number

2.         date              

3.         2013-01-01      51

4.         2014-01-01     453

5.         2015-01-01     743

6.         2016-01-01    1552

7.         2017-01-01      92

  • 關(guān)于日期的類型,按參考下圖所示來選擇合適的分期頻率:

4.2 按日期統(tǒng)計后,按年或季度或月份顯示

按年統(tǒng)計并顯示

1.         print(df.resample('AS').sum().to_period('A'))

2.         # 按年統(tǒng)計并顯示

1.               number

2.         date        

3.         2013      51

4.         2014     453

5.         2015     743

6.         2016    1552

7.         2017      92

按季度統(tǒng)計并顯示

1.         print(df.resample('Q').sum().to_period('Q').head())

2.         # 按季度統(tǒng)計并顯示

1.                 number

2.         date          

3.         2013Q4      51

4.         2014Q1      73

5.         2014Q2      96

6.         2014Q3     136

7.         2014Q4     148

按月度統(tǒng)計并顯示

1.         print(df.resample('M').sum().to_period('M').head())

2.         # 按月度統(tǒng)計并顯示

1.                  number

2.         date          

3.         2013-10      10

4.         2013-11      14

5.         2013-12      27

6.         2014-01      16

7.         2014-02       4

以上是“Pandas中怎么按日期篩選、顯示及統(tǒng)計數(shù)據(jù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI