I have the temperatures being measuered and I want to create a heatmap from it. For this I first have to create a DataFrame where hour and 15 minute intervals are on the Index and the column.
The source data is like this:
date | temperature |
---|---|
2021-08-14 11:14:00 | 27.8 |
2021-08-14 11:15:00 | 27.9 |
2021-08-14 11:16:00 | 27.9 |
2021-08-14 11:17:00 | 27.9 |
2021-08-14 11:18:00 | 27.9 |
.... | .... |
2021-08-14 11:31:00 | 28.10 |
2021-08-14 11:32:00 | 28.10 |
2021-08-14 11:33:00 | 28.10 |
2021-08-14 11:34:00 | 28.10 |
What I want to get is:
date | 00 | 15 | 30 | 45 |
---|---|---|---|---|
11:00 | 27.8 | 27.9 | 28.1 | 28.3 |
12:00 | .. | .. | .. | .. |
So I want the time intervals within the hour to be split on the columns and the index containing the specific hours (on which the columns occur).
Is there any way to do this action in Pandas in an easy way?
Thanks in advance!
Let's first separate hours and minutes (rounded to 15), put them back in the dataframe and use .pivot_table()
to build your dataframe with interval means:
>>> h = df['date'].dt.strftime('%H:00').rename('hour')
>>> m = df['date'].dt.floor(freq='15T').dt.minute.rename('minutes')
>>> df.join([h, m]).pivot_table(index='hour', columns='minutes', values='temperature', aggfunc='mean')
minutes 0 15 30 45
hour
2021-08-14 11:00:00 28.709492 28.026066 27.991953 28.096947
2021-08-14 12:00:00 27.877558 28.022282 27.720347 28.201100
2021-08-14 13:00:00 27.739935 NaN NaN NaN
Use resample
and pivot_table
to get expected outcome:
out = df.set_index('date').resample('15T').mean()
out = pd.pivot_table(out, index=out.index.strftime('%H:00'),
columns=out.index.strftime('%M'),
values='temperature')
out = out.rename_axis(index=None, columns=None)
>>> out
00 15 30
11:00 27.8 27.9 28.1
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.