【问题标题】:For loops in pandas dataframe, how to filter by days of hourly range and associate a value?对于熊猫数据框中的循环,如何按小时范围内的天数过滤并关联一个值?
【发布时间】:2022-01-09 15:39:40
【问题描述】:

我有一个名为 df_sub 的数据框,如下所示:

                   date open    high    low close   volume  
405 2022-01-03 08:00:00 4293.5  4295.5  4291.5  
406 2022-01-03 08:01:00 4294.0  4295.5  4294.0  
407 2022-01-03 08:02:00 4295.5  4297.5  4295.5  
408 2022-01-03 08:03:00 4297.0  4298.0  4296.0  
409 2022-01-03 08:04:00 4296.5  4296.5  4295.0  
... ... ... ... ... ... ... ... ...
5460    2022-01-07 08:55:00 4311.0  4312.0  4310.5  
5461    2022-01-07 08:56:00 4311.5  4311.5  4311.0  
5462    2022-01-07 08:57:00 4311.0  4312.0  4310.0  

    

我需要创建一个这种类型的循环:

for row in df_sub:
take a single day (so, in this case 2022-01-03, 04...07) and create a column with df_sub["high"].max() value, 
so i will have the maximum value of the high in all the rows of the same day,
naturally, this implies that in other day the maximum value will be different from the 
previews one because the high will be different. 

【问题讨论】:

    标签: python pandas dataframe filter


    【解决方案1】:

    你可以使用resample:

    df_sub=df_sub.set_index('date')
    df_new=df_sub.resample('d')['high'].max()
    

    【讨论】:

    • 感谢您的回答,但我怎样才能创建一个像 df_sub["max_value"] 这样的新列,其中有最大值的值(我不在乎这种方式是否有多个值一天中的最高行数。我需要此列以简单的方式执行其他任务
    • 在 df_sub 中使用 for n: df_sub["max_value"] = df_sub.resample('d')['high'].max() 我得到了 nan 值
    猜你喜欢
    • 2019-04-13
    • 2022-11-18
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 2021-12-12
    相关资源
    最近更新 更多