gpt4 book ai didi

python - 具有本地化日期时间索引的数据框 : how to drop days not having a given time

转载 作者:行者123 更新时间:2023-12-01 06:53:07 24 4
gpt4 key购买 nike

我有一个如下所示的数据框:

                            A   B
2014-06-02 09:00:00-04:00 ... ...
2014-06-02 10:00:00-04:00 ... ...
2014-06-02 11:00:00-04:00 ... ...
2014-06-02 12:00:00-04:00 ... ...
2014-06-03 09:00:00-04:00 ... ...
2014-06-03 10:00:00-04:00 ... ...
2014-06-03 11:00:00-04:00 ... ...
2014-06-04 09:00:00-04:00 ... ...
2014-06-04 10:00:00-04:00 ... ...
2014-06-04 11:00:00-04:00 ... ...
2014-06-04 12:00:00-04:00 ... ...

我需要删除没有时间 12:00:00-04:00 的日子:在我的示例中,它将是 2014-06-03。所以最终的数据框看起来像:

                            A   B
2014-06-02 09:00:00-04:00 ... ...
2014-06-02 10:00:00-04:00 ... ...
2014-06-02 11:00:00-04:00 ... ...
2014-06-02 12:00:00-04:00 ... ...
2014-06-04 09:00:00-04:00 ... ...
2014-06-04 10:00:00-04:00 ... ...
2014-06-04 11:00:00-04:00 ... ...
2014-06-04 12:00:00-04:00 ... ...

请注意,索引已本地化 (-04:00)

Pandas 0.24.2

最佳答案

您可以分组过滤

df.groupby(df.index.date).filter(lambda s: 12 in s.index.hour)

                            A   B
2014-06-02 09:00:00-04:00 ... ...
2014-06-02 10:00:00-04:00 ... ...
2014-06-02 11:00:00-04:00 ... ...
2014-06-02 12:00:00-04:00 ... ...
2014-06-04 09:00:00-04:00 ... ...
2014-06-04 10:00:00-04:00 ... ...
2014-06-04 11:00:00-04:00 ... ...
2014-06-04 12:00:00-04:00 ... ...

关于python - 具有本地化日期时间索引的数据框 : how to drop days not having a given time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58918666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com