熊猫 - 将行日期与列时间相结合

时间:2017-01-02 10:47:15

标签: python pandas multiple-columns reshape timedelta

我有一个数据框:

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
                 ...

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

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

1 个答案:

答案 0 :(得分:3)

您可以使用:

#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