Pandas - 将行日期与列时间结合起来 [英] Pandas - combine row dates with column times

查看:77
本文介绍了Pandas - 将行日期与列时间结合起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框:

Date          0:15     0:30     0:45   ...    23:15    23:30    23:45   24:00
2004-05-01  3.74618  3.58507  3.30998  ...  2.97236  2.92008  2.80101  2.6067
2004-05-02  3.09098  3.84625  3.54672  ...  2.83725  2.93876  2.82762  2.6255

如何将其转换为:

Date                value
2004-05-01 0:15     3.74618
2004-05-01 0:30     3.58507
2004-05-01 0:45     3.30998
                 ...

我写了一些确实有效的代码,但我相信可以用更优雅的几行代码来做同样的事情

I wrote some code which does work, but I'm sure it's possible to do the same in more elegant couple of lines code

cols = []
for col in frame.columns.values:
    if col != '24:00':
        dt = self.datetime(col, '%H:%M')
        td = timedelta(hours=dt.hour, minutes=dt.minute)
    else: td = timedelta(days=1)
    dt1 = date + td
    cols.append(dt1)

frame.columns = cols
frame = frame.T

推荐答案

您可以使用:

#convert if dtype not datetime
df.Date = pd.to_datetime(df.Date)
#create index from column Date
df = df.set_index('Date')
#convert all times to timedelta
df.columns = pd.to_timedelta(df.columns + ':00', unit='h')
#reshape - columns to second level of index
df = df.stack()
#Multindex to index
df.index = df.index.get_level_values(0) + df.index.get_level_values(1)
#create column from index
df = df.reset_index()
#get new column names
df.columns = ['date','val']
print (df)
                  date      val
0  2004-05-01 00:15:00  3.74618
1  2004-05-01 00:30:00  3.58507
2  2004-05-01 00:45:00  3.30998
3  2004-05-01 23:15:00  2.97236
4  2004-05-01 23:30:00  2.92008
5  2004-05-01 23:45:00  2.80101
6  2004-05-02 00:00:00  2.60670
7  2004-05-02 00:15:00  3.09098
8  2004-05-02 00:30:00  3.84625
9  2004-05-02 00:45:00  3.54672
10 2004-05-02 23:15:00  2.83725
11 2004-05-02 23:30:00  2.93876
12 2004-05-02 23:45:00  2.82762
13 2004-05-03 00:00:00  2.62550

这篇关于Pandas - 将行日期与列时间结合起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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