I have a simple Pandas DataFrame that looks like this:
Timestamp Value
2017-01-03 00:00:00 13.05
2017-01-03 00:01:00 13.70
2017-01-03 00:02:00 13.07
...
2017-01-27 09:57:00 13.46
2017-01-27 09:58:00 13.43
2017-01-27 09:59:00 13.41
How can I transpose it so as to have a single row per day, and columns for specific hours and minutes (say from 0 to 10 hours) (there are no seconds in the Timestamp, they are all zeros).
So far, I can successfully use df[df.index.time < datetime.time(10,0)]
to select the rows for each day below hour 10. But if I subsequently do .T
to transpose that selection, then the entire DataFrame gets transposed into one very long row. How can I perform that .T
operation on batches of rows for each distinct day from the Timestamp? (and also possibly put each Day value in the first column of the transposed dataset?
I would rather avoid custom code and looping, as the dataset is large, so something built-in is much preferred!
You can use pivot_table method:
In [214]: df.assign(d=df.Timestamp.dt.date, t=df.Timestamp.dt.time) \
.pivot_table(index='d', columns='t', values='Value', fill_value=0) \
.rename_axis(None, 1)
Out[214]:
00:00:00 00:01:00 00:02:00 09:57:00 09:58:00 09:59:00
2017-01-03 13.05 13.7 13.07 0.00 0.00 0.00
2017-01-27 0.00 0.0 0.00 13.46 13.43 13.41
PS but I would prefer @piRSquared's solution as it's more elegant...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.