为什么groupby.apply返回重复级别 [英] why groupby.apply return duplicate level
本文介绍了为什么groupby.apply返回重复级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def afun(group):
aa=len(group)
group.sort_values(inplace=True)
return pandas.DataFrame({'score':numpy.arange(aa),'price':group})
df = pandas.DataFrame({
'stock':numpy.repeat( ['AAPL','GOOG','YHOO'], 3 ),
'date':numpy.tile( pandas.date_range('5/5/2015', periods=3, freq='D'), 3 ),
'price':(numpy.random.randn(9).cumsum() + 10) ,
'price2':(numpy.random.randn(9).cumsum() + 10)})
df = df.set_index(['stock','date'])
agroupDf=df.groupby(level='date')
tt=agroupDf['price'].apply(afun)
变量tt的值如图所示
我的问题是为什么tt有两列日期",以及如何避免第二列日期"?
my question is why tt has two columns 'date',and how to avoid the secend column 'date'?
推荐答案
第一个是groupby
'date'.第二个是index
'日期'.
The first one is the groupby
'date'. The second one is the index
'date'.
改变事物-这次groupby
库存:
df = df.set_index(['date','stock'])
agroupDf = df.groupby(level='stock')
tt=agroupDf['price'].apply(afun)
tt
price score
stock date stock
AAPL 2015-05-05 AAPL 9.333143 0
2015-05-06 AAPL 9.680022 1
2015-05-07 AAPL 9.870889 2
GOOG 2015-05-06 GOOG 10.030032 0
2015-05-05 GOOG 10.229084 1
2015-05-07 GOOG 10.571631 2
YHOO 2015-05-07 YHOO 9.996925 0
2015-05-05 YHOO 10.342180 1
2015-05-06 YHOO 10.586120 2
我想你想要这个:
df = df.set_index('stock')
agroupDf = df.groupby('date')
tt=agroupDf['price'].apply(afun)
tt
price score
date stock
2015-05-05 AAPL 10.414396 0
GOOG 12.608225 1
YHOO 12.830496 2
2015-05-06 AAPL 10.428767 0
GOOG 11.189663 1
YHOO 11.988177 2
2015-05-07 YHOO 11.202677 0
AAPL 11.274440 1
GOOG 11.780654 2
这篇关于为什么groupby.apply返回重复级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文