根据时间python查找最大值和下降行

发布于 2025-02-07 12:58:01 字数 1274 浏览 4 评论 0原文

我试图根据电台列和有效列的小时找到REC的最大值。我没有可行的代码,因为我一直在尝试这种情况,而且还没有发现工作解决方案。

电台合作
数据正在
​02-23 08:07:000.1
1V42022-02-23 08:51:000.6
BTV2022-02-23 07:52:000.1
BTV2022-02-2307
:54:54: 00: 54 23 07:59:000.3
BTV2022-02-23 08:02:000.0
BTV2022-02-23 08:16:000.0
BTV08:29:000.3

2022-02-23 看起来像

有效爆炸
1V42022-02-23 07:58:000.4
1V42022-02-23 08:51:000.6
BTV2022-02-23 07:59:000.3 0.3
BTV20222-202-23 08:29 :000.3

I am trying to find the maximum value of precip based on the station column and the hour of the valid column. I have no workable code as I've been trying this forever and have found no even close to working solution.

Here is the dataframe I am working with

stationvalidprecip
1V42022-02-23 07:54:000.2
1V42022-02-23 07:55:000.2
1V42022-02-23 07:58:000.4
1V42022-02-23 08:07:000.1
1V42022-02-23 08:51:000.6
BTV2022-02-23 07:52:000.1
BTV2022-02-23 07:54:000.2
BTV2022-02-23 07:59:000.3
BTV2022-02-23 08:02:000.0
BTV2022-02-23 08:16:000.0
BTV2022-02-23 08:29:000.3

This is what I want it to look like

stationvalidprecip
1V42022-02-23 07:58:000.4
1V42022-02-23 08:51:000.6
BTV2022-02-23 07:59:000.3
BTV2022-02-23 08:29:000.3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2025-02-14 12:58:01

您需要按车站和小时分组,并获取idxmax,然后切片:

df['valid'] = pd.to_datetime(df['valid'])

df.loc[df.groupby(['station', df['valid'].dt.hour])['precip'].idxmax()]

输出:

   station               valid  precip
2      1V4 2022-02-23 07:58:00     0.4
4      1V4 2022-02-23 08:51:00     0.6
7      BTV 2022-02-23 07:59:00     0.3
10     BTV 2022-02-23 08:29:00     0.3

You need to group by station and hour and get the idxmax, then slice:

df['valid'] = pd.to_datetime(df['valid'])

df.loc[df.groupby(['station', df['valid'].dt.hour])['precip'].idxmax()]

output:

   station               valid  precip
2      1V4 2022-02-23 07:58:00     0.4
4      1V4 2022-02-23 08:51:00     0.6
7      BTV 2022-02-23 07:59:00     0.3
10     BTV 2022-02-23 08:29:00     0.3
極樂鬼 2025-02-14 12:58:01

这将使您能够按小时和车站进行分组

df1['Hour'] = pd.to_datetime(df['valid'], infer_datetime_format=True).dt.hour
df1['precip_max'] = df1.groupby(['station', 'Hour'])['precip'].transform('max')
df1.loc[df['precip'] == df['precip_max']]

This will allow you to group by the hour and the station

df1['Hour'] = pd.to_datetime(df['valid'], infer_datetime_format=True).dt.hour
df1['precip_max'] = df1.groupby(['station', 'Hour'])['precip'].transform('max')
df1.loc[df['precip'] == df['precip_max']]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文