简体   繁体   中英

How to split weekly data to hourly?

I have to dataframes where I have in one column weekly data and in the other data hourly, such as this:

datetime                |   A
------------------------|-------------
06/01/2020  00:00:00    | 130
13/01/2020  00:00:00    |  80
.....................................
21/12/2020  00:00:00    | 234
28/12/2020  00:00:00    | 123

and hourly :

datetime                |   A
------------------------|-------------
06/01/2020  00:00:00    |   
06/01/2020  01:00:00    |
................................
27/12/2020  22:00:00    |   
27/12/2020  23:00:00    |

I would like to fill the hourly data by dividing the weekly data for each respective week by 7*24 (hours in a week). I think that should be relatively straight forward but I am currently struggling a lot while trying to implement it. How would the best way be to implement this?

To clarify I would like something like:

datetime                |   A
------------------------|-------------
06/01/2020  00:00:00    |  80/7/24 
06/01/2020  01:00:00    |  80/7/24 
................................
27/12/2020  22:00:00    |  123/7/24 
27/12/2020  23:00:00    |  123/7/24 

You could use the following:

df2 = (df_week.assign(date = pd.to_datetime(df_week['datetime']).dt.date)
  .merge(df_hour.assign(date = pd.to_datetime(df_hour['datetime']).dt.date), on = 'date', suffixes=['_x', ''])
  .assign(A = lambda x: x.A_x/7/24)[['datetime', 'A']])
df2

               datetime        A
0  06/01/2020  00:00:00  0.77381
1  06/01/2020  01:00:00  0.77381

breaking this down:

df_week['date'] = pd.to_datetime(df_week['datetime']).dt.date
df_hour['date'] = pd.to_datetime(df_hour['datetime']).dt.date
df2 = df_week.merge(df_hour, on = 'date', suffixes=['_x', ''])
df_hour['A'] = df2['A_x']/7/24
df_hour

               datetime        A        date
0  06/01/2020  00:00:00  0.77381  2020-06-01
1  06/01/2020  01:00:00  0.77381  2020-06-01

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