简体   繁体   中英

Pandas rearrange hour-date excel table into a datetime dataframe

I have an Excel data set that looks like this:

    24      25      26      27
1   0,3818  0,0713  0,07222 0,3542
2   0,17802 0,04508 0,06877 0,17319
3   0,22356 0,07314 0,04991 0,22448
4   0,1771  0,07038 0,07406 0,19136
5   0,19389 0,06164 0,05497 0,18538
6   0,20401 0,07475 0,06417 0,21413
7   0,18354 0,07245 0,07337 0,17756
8   0,46184 0,04669 0,0506  0,28819
9   0,43838 0,0667  0,06785 0,4692
10  0,78292 0,07038 0,07291 0,66424
11  1,81792 0,06003 0,04508 1,17001
12  2,40833 0,05451 0,07245 1,08422
13  1,55746 0,07038 0,07314 0,61272
14  1,2075  0,06509 0,04485 0,40871
15  2,4196  0,05014 0,07291 0,27393
16  0,95979 0,07015 0,07291 0,2323
17  0,51681 0,06992 0,04554 0,2024
18  0,46529 0,04232 0,85192 0,35558
19  0,58328 0,06992 1,59321 0,60283
20  1,40185 0,07015 0,82869 1,23326
21  0,71484 0,04692 1,05041 1,01131
22  0,48576 0,07291 0,80707 1,4697
23  0,04278 0,07245 0,57523 1,72316
24  0,07291 0,04554 0,5175  0,61364

The first column represents the hours of the day, the first row the number of day of the year (24 corresponds to the 24th of January, the rows spans all the way through the year, ending on the day number 365) for the year 2013.

What I want to obtain is a dataframe which as first column has the date, with year-month-day-hour and for which the respective hourly value is correctly associated.

'date'            'value'
2013-01-24 01:00  0.3818
2013-01-24 02:00  0.17802
2013-01-24 03:00  0.22356
...

The Excel data set

Thank you for your help.

This is the best I've got:

if your data is on a pandas.DataFrame called df you can do:

df2 = df.unstack()
start = pd.Timestamp('01/01/2013')
df2 = df2.reset_index()
df2['date'] = [start + pd.DateOffset(days = int(x)-1) for x in  df2.level_0.values]
df2['date'] +=  pd.to_timedelta(df2.level_1, unit='h')
df2.index = df2.date
df2 = df2[0]

Result

    date
2013-01-24 00:00:00     0,3818
2013-01-24 01:00:00    0,17802
2013-01-24 02:00:00    0,22356
2013-01-24 03:00:00     0,1771
2013-01-24 04:00:00    0,19389
2013-01-24 05:00:00    0,20401
2013-01-24 06:00:00    0,18354
2013-01-24 07:00:00    0,46184
2013-01-24 08:00:00    0,43838
2013-01-24 09:00:00    0,78292
2013-01-24 10:00:00    1,81792
2013-01-24 11:00:00    2,40833
2013-01-24 12:00:00    1,55746
2013-01-24 13:00:00     1,2075
2013-01-24 14:00:00     2,4196
2013-01-24 15:00:00    0,95979
2013-01-24 16:00:00    0,51681
2013-01-24 17:00:00    0,46529
2013-01-24 18:00:00    0,58328
2013-01-24 19:00:00    1,40185
2013-01-24 20:00:00    0,71484
2013-01-24 21:00:00    0,48576
2013-01-24 22:00:00    0,04278
2013-01-24 23:00:00    0,07291
2013-01-25 00:00:00     0,0713

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM