gpt4 book ai didi

python - Pandas ,删除重复的日期时间

转载 作者:行者123 更新时间:2023-11-28 22:44:23 24 4
gpt4 key购买 nike

我有一个这样的数据框。

       timestamp         avg_hr  avg_rr    emfit_sleep_summary_id   AVG_HR    AVG_RR
2015-01-28 08:14:50 101 6.4 78 99.5 6.4
2015-01-28 08:14:52 98 6.4 78 99.5 6.4
2015-01-28 00:25:00 60 0.0 78 61.5 0.0
2015-01-28 00:25:02 63 0.0 78 61.5 0.0
2015-01-28 07:24:06 79 11.6 78 78.5 11.6
2015-01-28 07:24:08 79 11.6 78 78.5 11.6

其中时间戳就是索引。我正在尝试删除重复的时间戳(按小时),因此我尝试获取如下所示的内容而不是上表。

      timestamp         avg_hr  avg_rr    emfit_sleep_summary_id    AVG_HR    AVG_RR
2015-01-28 08:14:50 101 6.4 78 99.5 6.4
2015-01-28 00:25:02 63 0.0 78 61.5 0.0
2015-01-28 07:24:08 79 11.6 78 78.5 11.6

我尝试了以下方法。

df2 = df2.drop_duplicates(subset=df2.index.hour,take_last=True)

但是返回一个 s Keyerror: 22

我也尝试过:df2 = df2.drop_duplicates(subset=df2.index,take_last=True)

返回,KeyError: Timestamp('2015-03-31 22:29:48')

关于如何实现这一点有什么建议吗?

最佳答案

使用重采样:

In [52]:

df.resample('H', how='first').dropna(how='all')
Out[52]:
avg_hr avg_rr emfit_sleep_summary_id AVG_HR AVG_RR
timestamp
2015-01-28 00:00:00 60 0.0 78 61.5 0.0
2015-01-28 07:00:00 79 11.6 78 78.5 11.6
2015-01-28 08:00:00 101 6.4 78 99.5 6.4

使用 .dropna() 的原因是,每小时重新采样会在第一个和最后一个小时之间的每个小时创建行,如果该小时内没有数据,这些行将用 NaN 填充。

关于python - Pandas ,删除重复的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29559592/

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