使用DatetimeIndex重塑Pandas数据框以制作网格 [英] Reshape Pandas dataframe with DatetimeIndex to make grid

查看:88
本文介绍了使用DatetimeIndex重塑Pandas数据框以制作网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a pandas dataframe with a DatetimeIndex, e.g.

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,.....]作为列.

I want to reshape the dataframe so it has dates as the index and hours [0,0.5,1.0,1.5,.....] as the columns.

我有这个:

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

但是它给了我key error并带有日期列表not in index

but it gives me a key error with a list of dates 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

这篇关于使用DatetimeIndex重塑Pandas数据框以制作网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆