【问题标题】:Array of values in exact hour everyday(5 days)每天精确小时的值数组(5 天)
【发布时间】:2018-01-29 07:37:57
【问题描述】:

我有一个数组,比如:

                            0         1         2
date                                    
2000-06-01 10:00:00  0.040457  0.326594  0.492136 
2000-06-01 11:00:00  0.279323  0.877446  0.464523
2000-06-02 10:00:00  0.328068  0.837669  0.608559
2000-06-02 11:00:00  0.107959  0.678297  0.517435
2000-06-03 10:00:00  0.131555  0.418380  0.025725
2000-06-03 11:00:00  0.999961  0.619517  0.206108
2000-06-04 10:00:00  0.129270  0.024533  0.154769
2000-06-04 11:00:00  0.441010  0.741781  0.470402
2000-06-05 10:00:00  0.682101  0.375660  0.009916
2000-06-05 11:00:00  0.754488  0.352293  0.339337

我需要在每天的每个小时接收每个值的数组。 它应该是数据框

10:00:00  [[0.040457, 0.040457, 0.492136], [0.328068, 0.837669, 0.608559], ..., [0.682101, 0.375660, 0.009916]]
11:00:00  [[0.279323, 0.877446, 0.464523], [0.107959, 0.678297, 0.517435], ..., [0.754488, 0.352293, 0.339337]]

我试过这个:

locs = a.index.indexer_at_time('11:00:00')
a.iloc[locs]

但它没有显示我需要什么。可能有什么方法可以通过 numpy 做到这一点吗? 重要提示:不应使用循环。如果也是,如果可能的话

【问题讨论】:

    标签: python arrays pandas numpy dataframe


    【解决方案1】:

    对数据框应用一个函数,该函数将通过比较时间来过滤值。

    output = []
    
    
    def filter_data(row):
        values = [row[0], row[1], row[2]]
        if row.date.split()[1] == '10:00:00':
            output.append(values)
    
    df.apply(filter_data, axis=1)
    
    print (output)
    

    【讨论】:

      【解决方案2】:

      您可以使用数据透视表通过创建“小时”列来做到这一点

      df['hour'] = df.reset_index()['date'].dt.hour.values
      ndf = df.pivot_table(index=df.hour, columns=df.groupby(df['hour']).cumcount(),values=df[[0,1,2]])
      

      输出:

      0 1 \ 0 1 2 3 4 0 1 小时 10 0.040457 0.328068 0.131555 0.12927 0.682101 0.326594 0.837669 11 0.279323 0.107959 0.999961 0.44101 0.754488 0.877446 0.678297 2\ 2 3 4 0 1 2 3 小时 10 0.418380 0.024533 0.375660 0.492136 0.608559 0.025725 0.154769 11 0.619517 0.741781 0.352293 0.464523 0.517435 0.206108 0.470402 4 小时 10 0.009916 11 0.339337

      您还可以使用 .loc 来获取特定时间数据,即

      ndf.loc[10]
      

      输出:

      0 0 0.040457 1 0.328068 2 0.131555 3 0.129270 4 0.682101 1 0 0.326594 1 0.837669 2 0.418380 3 0.024533 4 0.375660 2 0 0.492136 1 0.608559 2 0.025725 3 0.154769 4 0.009916 名称:10,数据类型:float64

      【讨论】:

        【解决方案3】:

        假设我理解正确,并且您想要的是一个对象,该对象包含数据中每个不同小时的一行,每行包含一个数组,其中包含 0、1、2 列的所有数据值作为数组,以下将执行此操作:

        #get the hour as a column
        x['hour'] = x.date.dt.hour
        
        #groupby hour
        #use apply to get values
        #filter to only columns you want in the values:
        by_hours = x.groupby('hour').apply(lambda x: x[['0','1','2']].values)
        

        返回:

        hour
        10    [[0.040457, 0.326594, 0.492136], [0.328068, 0....
        11    [[0.279323, 0.877446, 0.464523], [0.107959, 0....
        dtype: object
        

        现在您可以按如下方式访问每个“小时”内的值:

        by_hours.loc[10]
        

        返回:

        array([[ 0.040457,  0.326594,  0.492136],
               [ 0.328068,  0.837669,  0.608559],
               [ 0.131555,  0.41838 ,  0.025725],
               [ 0.12927 ,  0.024533,  0.154769],
               [ 0.682101,  0.37566 ,  0.009916]])
        

        【讨论】:

        • 我收到一个错误:“DatetimeIndex”对象没有属性“dt”。我改用 .time,但时间没有小时
        • 如果您使用df.reset_index(inplace=False, drop=False),那么日期列将是常规列而不是索引。之后,这应该可以工作。
        猜你喜欢
        • 2019-03-28
        • 1970-01-01
        • 1970-01-01
        • 2022-07-15
        • 1970-01-01
        • 1970-01-01
        • 2016-09-10
        • 2019-08-13
        • 1970-01-01
        相关资源
        最近更新 更多