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 用法一样的
#分组标准化
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)