pandas groupby, resample 按時間取樣

SHOUGOUGOU發表於2020-11-27

pandas 給時間劃分割槽間有幾種相似的方式

1.period_range

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.period_range.html

2. pandas Grouper  按時間取樣分組,引數和resample類似

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Grouper.html

3.resample  和groupby 類似

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.resample.html

closed{‘right’, ‘left’}, default None

Which side of bin interval is closed. The default is ‘left’ for all frequency offsets except for ‘M’, ‘A’, ‘Q’, ‘BM’, ‘BA’, ‘BQ’, and ‘W’ which all have a default of ‘right’.

label{‘right’, ‘left’}, default None

Which bin edge label to label bucket with. The default is ‘left’ for all frequency offsets except for ‘M’, ‘A’, ‘Q’, ‘BM’, ‘BA’, ‘BQ’, and ‘W’ which all have a default of ‘right’.

 

4.groupby.transform  分組執行函式

resample.transform 用法一樣的

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.DataFrameGroupBy.transform.html

#分組標準化
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
                          'foo', 'bar'],
                   'B' : ['one', 'one', 'two', 'three',
                          'two', 'two'],
                   'C' : [1, 5, 5, 2, 5, 5],
                   'D' : [2.0, 5., 8., 1., 2., 9.]})
grouped = df.groupby('A')
grouped.transform(lambda x: (x - x.mean()) / x.std())
          C         D
0 -1.154701 -0.577350
1  0.577350  0.000000
2  0.577350  1.154701
3 -1.154701 -1.000000
4  0.577350 -0.577350
5  0.577350  1.000000

#得到分組的groupname
grouped.transform(lambda x: x.name)

 

相關文章