【问题标题】:Reshape Pandas dataframe with DatetimeIndex to make grid使用 DatetimeIndex 重塑 Pandas 数据框以制作网格
【发布时间】:2017-11-03 08:00:46
【问题描述】:

我有一个带有 DatetimeIndex 的 pandas 数据框,例如

In: ts = pd.date_range('2013-01-01 00:00', periods=17520, freq='30min')
In: values = list(range(1, 17520))
In: df = pd.DataFrame(values, index=ts)

我想重塑数据框,使其将日期作为索引,将小时数 [0,0.5,1.0,1.5,.....] 作为列。

我有这个:

df.pivot(index=df.index.date, columns=df.index.time, values='values')

但它给了我一个key error 和日期列表not in index

【问题讨论】:

    标签: python pandas dataframe pivot reshape


    【解决方案1】:
    df.index = [df.index.date, df.index.hour + df.index.minute / 60]
    
    df[0].unstack()
    
                0.0  0.5  1.0  1.5  2.0  2.5  3.0  3.5  4.0  4.5
    2013-01-01    1    2    3    4    5    6    7    8    9   10
    2013-01-02   49   50   51   52   53   54   55   56   57   58
    2013-01-03   97   98   99  100  101  102  103  104  105  106
    2013-01-04  145  146  147  148  149  150  151  152  153  154
    2013-01-05  193  194  195  196  197  198  199  200  201  202
    2013-01-06  241  242  243  244  245  246  247  248  249  250
    2013-01-07  289  290  291  292  293  294  295  296  297  298
    2013-01-08  337  338  339  340  341  342  343  344  345  346
    2013-01-09  385  386  387  388  389  390  391  392  393  394
    2013-01-10  433  434  435  436  437  438  439  440  441  442
    

    【讨论】:

      【解决方案2】:

      您可以为此使用pd.crosstab

      pd.crosstab(df.index.date, df.index.time, df[0], aggfunc='sum')
      
      #col_0      00:00:00    00:30:00    01:00:00    01:30:00  ... more columns
      #row_0                                                                                  
      #2013-01-01        1           2           3           4
      #2013-01-02       49          50          51          52
      #... more rows
      

      【讨论】:

        猜你喜欢
        • 2012-12-10
        • 1970-01-01
        • 2023-03-07
        • 2020-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-21
        相关资源
        最近更新 更多