使用以下数据集
close_px = pd.read_csv(u'https://gitee.com/pan19/data-source/raw/master/stock_px_2.csv', parse_dates=True,
index_col=0)
close_px.info()
close_px.head(10)
如果lambda什么也不做,只是打印,可以看到groupby默认使用索引进行分组
show_index = lambda x: print(x)
by_index = close_px.groupby(show_index)
输出
DatetimeIndex([‘2003-01-02’, ‘2003-01-03’, ‘2003-01-06’, ‘2003-01-07’,
‘2003-01-08’, ‘2003-01-09’, ‘2003-01-10’, ‘2003-01-13’,
‘2003-01-14’, ‘2003-01-15’,
…
‘2011-10-03’, ‘2011-10-04’, ‘2011-10-05’, ‘2011-10-06’,
‘2011-10-07’, ‘2011-10-10’, ‘2011-10-11’, ‘2011-10-12’,
‘2011-10-13’, ‘2011-10-14’],
dtype=‘datetime64[ns]’, length=2214, freq=None)
使用索引中的年份分组
show_index = lambda x: x.year
by_index = close_px.groupby(show_index)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)