Reshape Pandas dataframe with DatetimeIndex to mak

2019-05-08 18:16发布

问题:

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)

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.

I have this:

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

but it gives me a key error with a list of dates not in index

回答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:

You can use pd.crosstab for this:

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