【问题标题】:How to filter hours in Pandas Dataframe如何在 Pandas Dataframe 中过滤小时数
【发布时间】:2021-03-03 02:38:02
【问题描述】:

如果我有一个 pandas 数据框,并且我想过滤每天的特定时间,例如 10:00 到 16:00 之间的所有数据

                   time      open      high       low     close  tick_volume  spread  real_volume
0   2021-02-23 15:25:00  114990.0  115235.0  114980.0  115185.0        55269       5       235555
1   2021-02-23 15:30:00  115180.0  115215.0  115045.0  115135.0        31642       5       116914
2   2021-02-23 15:35:00  115135.0  115240.0  115055.0  115220.0        29381       5       116516
3   2021-02-23 15:40:00  115220.0  115300.0  115030.0  115060.0        46740       5       184703
4   2021-02-23 15:45:00  115055.0  115075.0  114785.0  114885.0        48185       5       200241
5   2021-03-02 15:40:00  111680.0  111895.0  111580.0  111825.0        38471       5       144735
6   2021-03-02 16:15:00  111820.0  112500.0  111750.0  112270.0        71153       5       278122

怎么做?

【问题讨论】:

    标签: python pandas dataframe filter


    【解决方案1】:

    这应该可以解决问题:

    import pandas as pd
    df = pd.read_excel(path)
    df['time'] = pd.to_datetime(df['time']) #convert column to datetime if not already in that format
    df.set_index(['time'], inplace=True) #temporarily put time column into index
    df = df.between_time('10:00','16:00') #filter between times
    df = df.reset_index() #reset the index to make time a column again
    

    【讨论】:

      猜你喜欢
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 2020-11-29
      • 2012-10-21
      • 2013-12-12
      • 1970-01-01
      • 2019-01-07
      相关资源
      最近更新 更多