使用DatetimeIndex重塑Pandas数据框以制作网格

医生

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

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

海盗
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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章