当DateTime索引不是唯一且对应的值相同时重新采样 [英] Resampling when the DateTime index is not unique and the corresponding value is the same
本文介绍了当DateTime索引不是唯一且对应的值相同时重新采样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下df
DataFrame(pandas
):
I have the following df
DataFrame (pandas
):
attribute
2017-01-01 a
2017-01-01 a
2017-01-05 b
2017-02-01 a
2017-02-10 a
其中第一列是一个非唯一的datetime
索引,我想每周计算a和b的数量.如果我尝试df.attribute.resample('W').count()
,将由于输入重复而出现错误.
where the first column is a non-unique datetime
index and I want to count the number of a's and b's on a weekly basis. If I try to df.attribute.resample('W').count()
there will be an error, because of duplicate entries.
我该怎么做?
推荐答案
您可能会对包含groupby
和resample
的两步过程感兴趣.
You might be interested in a 2-step process involving a groupby
followed by a resample
.
df.groupby(level=0).count().resample('W').sum()
attribute
2017-01-01 2.0
2017-01-08 1.0
2017-01-15 NaN
2017-01-22 NaN
2017-01-29 NaN
2017-02-05 1.0
2017-02-12 1.0
这篇关于当DateTime索引不是唯一且对应的值相同时重新采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文