pandas學習筆記(三)

2021-08-21 21:35:23 字數 3465 閱讀 1594

#生成乙個以2017-01開始,月為頻率的時間構造器

#pd.period()引數:乙個時間戳+freq

p=pd.period('2017',freq='m')

print(p)

#通過加減整數,將週期整體移動

print(p+1)

print(p-2)

輸出:

2017-01

2017-02

2016-11

prng=pd.period_range('1/1/2011','1/1/2012',freq='m')

print(prng)

periodindex([『2011-01』, 『2011-02』, 『2011-03』, 『2011-04』, 『2011-05』, 『2011-06』,』2011-07』, 『2011-08』, 『2011-09』, 『2011-10』, 『2011-11』, 『2011-12』,』2012-01』],

dtype=』period[m]』, freq=』m』)

p=pd.period('2017','a-dec')

print(p)

print(p.asfreq('m',how='start')) #等於how='s'

print(p.asfreq('d',how='end')) #等於how='e'

輸出:

2017

2017-01

2017-12-31

#每月最後一日,轉換為每月

r=pd.date_range('2017/1/1',periods=10,freq='m')

pr=pd.period_range('2017','2018',freq='m')

ts1=pd.series(np.random.rand(len(r)),index=r)

print(ts1.head())

print(ts1.to_period().head())

#每月轉化為每月第一天

ts2=pd.series(np.random.rand(len(pr)),index=pr)

print(ts2.head())

print(ts2.to_timestamp().head())

輸出:

2017-01-31 0.294556

2017-02-28 0.037492

2017-03-31 0.219091

2017-04-30 0.742907

2017-05-31 0.242779

freq: m, dtype: float64

2017-01 0.294556

2017-02 0.037492

2017-03 0.219091

2017-04 0.742907

2017-05 0.242779

freq: m, dtype: float64

2017-01 0.588072

2017-02 0.648728

2017-03 0.457712

2017-04 0.452702

2017-05 0.159034

freq: m, dtype: float64

2017-01-01 0.588072

2017-02-01 0.648728

2017-03-01 0.457712

2017-04-01 0.452702

2017-05-01 0.159034

freq: ms, dtype: float64

r=pd.date_range('2017/1','2017/3')

ts=pd.series(np.random.rand(len(r)))

print(ts.head())

print(ts[:2])

print(ts[::2])

將時間序列從乙個頻率轉換為另乙個頻率的過程,且會有資料的結合

降取樣:高頻資料->低頻資料,eg:以天為頻率轉換為以月為頻率

公升取樣:低頻資料->高頻資料,eg:以年為頻率的資料轉換為以月為頻率的資料

r=pd.date_range('20170101',periods=12)

ts=pd.series(np.arange(12),index=r)

print(ts)

ts_re=ts.resample('5d')

ts_re1=ts.resample('5d').sum() #聚合方法

print(ts.resample('5d').mean()) #求平均值

print(ts.resample('5d').max()) #求最大值

print(ts.resample('5d').median()) #求中值

print(ts.resample('5d').first()) #返回第乙個值

print(ts.resample('5d').last()) #返回最後乙個值

print(ts.resample('5d').min()) #求最小值

r=pd.date_range('20170101',periods=12)

ts=pd.series(np.arange(1,13),index=r)

print(ts)

print(ts.resample('5d').sum())

print(ts.resample('5d',closed='left').sum())

#colosed:指定間隔,可以選左閉合或者右閉合

print(ts.resample('5d',label='left').sum())

#label:聚合值的index,預設取左

r=pd.date_range('2017/1/1 0:0:0',periods=5,freq='h')

ts=pd.dataframe(np.arange(15).reshape(5,3),index=r,columns=['a','b','c'])

print(ts)

print(ts.resample('15t').asfreq()) #不做填充返回nan

print(ts.resample('15t').ffill()) #向上填充

print(ts.resample('15t').bfill()) #向下填充

pr=pd.period_range('2016','2017',freq='m')

ts=pd.series(np.arange(len(pr)),index=pr)

print(ts.resample('3m').sum()) #降取樣

print(ts.resample('15d').ffill()) #公升取樣

pandas學習筆記 三

import pandas as pd import numpy as npdata pd.read csv student.csv data.head 學號姓名 班級年齡 01.0 小明1.0 7.01 2.0小華 1.06.0 23.0 小紅2.0 6.03 4.0小麗 3.08.0 45.0 ...

pandas基礎學習筆記三

二 groupby函式 三 聚合 過濾和變換 第3章 分組.經過groupby後會生成乙個groupby物件,該物件本身不會返回任何東西,只有當相應的方法被呼叫才會起作用 grouped single df.groupby school 根據某一列分組 grouped single.get grou...

pandas學習筆記

import numpy as np import pandas as pd obj2 pd.series 4,7,5,3 index d b a c obj2 out 99 d 4 b 7 a 5 c 3 dtype int64 a b pd.series a bout 102 a 1 b 2 c...