时间序列数据重组

发布于 2025-01-14 12:52:40 字数 461 浏览 1 评论 0原文

我正在编写一些可以重新排列时间序列的代码。目前我有一个标准时间序列。我有一个三列,标题为[日期、时间、值]。我想重新格式化数据帧以使用日期进行索引,并使用带有时间的标题(即 0:00、1:00、...、23:00)。数据框将填充该值。

这是当前的数据框

output

在此处输入图像描述

本质上,我想将索引移动到一天,并通过列显示小时数。

谢谢,

I am working on some code that will rearrange a time series. Currently I have a standard time series. I have a three columns with with the header being [Date, Time, Value]. I want to reformat the dataframe to index with the date and use a header with the time (i.e. 0:00, 1:00, ... , 23:00). The dataframe will be filled in with the value.

Here is the Dataframe currently have

output

enter image description here

essentially I'd like to mve the index toa single day and show the hours through the columns.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一向肩并 2025-01-21 12:52:40

使用pivot

df = df.pivot(index='Date', columns='Time', values='Total')

输出(前 10 列,Total 具有随机值):

>>> df.pivot(index='Date', columns='Time', values='Total').iloc[0:10]
time        00:00:00  01:00:00  02:00:00  03:00:00  04:00:00  05:00:00  06:00:00  07:00:00  08:00:00  09:00:00
date                                                                                                          
2019-01-01  0.732494  0.087657  0.930405  0.958965  0.531928  0.891228  0.664634  0.432684  0.009653  0.604878
2019-01-02  0.471386  0.575126  0.509707  0.715290  0.337983  0.618632  0.413530  0.849033  0.725556  0.186876

Use pivot:

df = df.pivot(index='Date', columns='Time', values='Total')

Output (first 10 columns and with random values for Total):

>>> df.pivot(index='Date', columns='Time', values='Total').iloc[0:10]
time        00:00:00  01:00:00  02:00:00  03:00:00  04:00:00  05:00:00  06:00:00  07:00:00  08:00:00  09:00:00
date                                                                                                          
2019-01-01  0.732494  0.087657  0.930405  0.958965  0.531928  0.891228  0.664634  0.432684  0.009653  0.604878
2019-01-02  0.471386  0.575126  0.509707  0.715290  0.337983  0.618632  0.413530  0.849033  0.725556  0.186876
人事已非 2025-01-21 12:52:40

你可以试试这个。
拆分时间部分以仅获取小时。添加 hr 到其中。

df = pd.DataFrame([['2019-01-01', '00:00:00',-127.57],['2019-01-01', '01:00:00',-137.57],['2019-01-02', '00:00:00',-147.57],], columns=['Date', 'Time', 'Totals'])
df['hours'] = df['Time'].apply(lambda x: 'hr'+ str(int(x.split(':')[0])))
print(pd.pivot_table(df, values ='Totals', index=['Date'], columns = 'hours'))

输出

hours          hr0     hr1
Date                      
2019-01-01 -127.57 -137.57
2019-01-02 -147.57     NaN

You could try this.
Split the time part to get only the hour. Add hr to it.

df = pd.DataFrame([['2019-01-01', '00:00:00',-127.57],['2019-01-01', '01:00:00',-137.57],['2019-01-02', '00:00:00',-147.57],], columns=['Date', 'Time', 'Totals'])
df['hours'] = df['Time'].apply(lambda x: 'hr'+ str(int(x.split(':')[0])))
print(pd.pivot_table(df, values ='Totals', index=['Date'], columns = 'hours'))

Output

hours          hr0     hr1
Date                      
2019-01-01 -127.57 -137.57
2019-01-02 -147.57     NaN
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文