Pandas - combine row dates with column times

半腔热情 提交于 2020-12-11 08:57:52

问题


I have a dataframe:

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

How do I convert it to:

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

回答1:


You can use:

#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


来源:https://stackoverflow.com/questions/41425326/pandas-combine-row-dates-with-column-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!