Python 使用'重新采样;how=count';引起问题
我有一个简单的熊猫数据框,它在不同的时间进行测量:Python 使用'重新采样;how=count';引起问题,python,pandas,Python,Pandas,我有一个简单的熊猫数据框,它在不同的时间进行测量: volume t 2013-10-13 02:45:00 17 2013-10-13 05:40:00 38 2013-10-13 09:30:00 29 2013-10-13 11:40:00 25 2013-10-13 12:50:00 11 2013-10-13 15:00:00 17 2013-10-13 17:10:00 1
volume
t
2013-10-13 02:45:00 17
2013-10-13 05:40:00 38
2013-10-13 09:30:00 29
2013-10-13 11:40:00 25
2013-10-13 12:50:00 11
2013-10-13 15:00:00 17
2013-10-13 17:10:00 15
2013-10-13 18:20:00 12
2013-10-13 20:30:00 20
2013-10-14 03:45:00 9
2013-10-14 06:40:00 30
2013-10-14 09:40:00 43
2013-10-14 11:05:00 10
我正在做一些基本的重采样和绘图,比如每天的总体积,效果很好:
df.resample('D',how='sum').head()
volume
t
2013-10-13 184
2013-10-14 209
2013-10-15 197
2013-10-16 309
2013-10-17 317
但由于某种原因,当我尝试每天计算条目的总数时,它会返回一个多索引系列,而不是数据帧:
df.resample('D',how='count').head()
2013-10-13 volume 9
2013-10-14 volume 9
2013-10-15 volume 7
2013-10-16 volume 9
2013-10-17 volume 10
我可以修复数据,以便通过简单的unstack调用轻松绘制数据,即
df.resample('D',how='count').unstack()
,但是,为什么使用how='count'
调用重采样与使用how='sum'
调用重采样有不同的行为?看起来,resample
和count
在结果数据帧的结构方面会导致一些奇怪的行为(至少是0.13.1)。有关稍微不同但相关的上下文,请参见此处:
您可以使用相同的策略
在这里:
这就是你的问题:
>>> df.resample('D',how='count')
2013-10-13 volume 9
2013-10-14 volume 4
您可以通过指定count
应用于volume
列,并在resample
调用中使用dict来解决此问题:
>>> df.resample('D',how={'volume':'count'})
volume
date
2013-10-13 9
2013-10-14 4
你找到解释了吗?这对于多指标尤其令人沮丧,因为我没有时间进一步研究,这里没有人回答,所以没有=(因此,我想我们可以假设这是一个bug。不过解决方法不错。有没有一种简单的方法可以在多个列中实现相同的结果?对我来说,这是可行的,但需要使用更多列进行大量键入:df.resample('D',how={'volume':'count','open':'count','high':'count})
how
参数现在不推荐使用此参数:
>>> df.resample('D',how={'volume':'count'})
volume
date
2013-10-13 9
2013-10-14 4