I would like to know how to fill up or replace the zero values in a column(e.g. consumption) with the same parameter value (e.g. consumtion) based on the total hourly average value of that parameter(e.g. consumption) in a time series data.
so, i have a datafreame something like this:
date consumption hour
2017-09-18 13:00:00 0.0 13
2017-09-18 14:00:00 14.0 14
2017-09-18 15:00:00 14.0 15
2017-09-18 16:00:00 12.0 16
2017-09-18 17:00:00 0.0 17
………………………………….
………………………………….
2022-01-01 19:00:00 0.0 19
2022-01-01 20:00:00 5.0 20
what i want, In the consumption column where there is zero values, i would like to replace them based on the hourly average value (for example: my hourly average value from the data is like below)
hour consumption
0 6.327579
1 5.144584
2 4.703064
3 4.495468
4 4.454467
5 5.791972
6 10.156371
7 11.922712
8 13.227804
9 14.803136
10 15.202267
11 14.928042
12 13.977903
13 13.386344
14 12.704201
15 12.259403
16 12.217954
17 13.464131
18 15.309193
19 15.211964
20 14.408146
21 12.503245
22 10.452741
23 8.195858
so this will be my expacted outcome
date consumption hour
2017-09-18 13:00:00 13.386344 13
2017-09-18 14:00:00 14.0 14
2017-09-18 15:00:00 14.0 15
2017-09-18 16:00:00 12.0 16
2017-09-18 17:00:00 13.464131 17
………………………………….
………………………………….
2022-01-01 19:00:00 15.211964 19
2022-01-01 20:00:00 5.0 20
I tried to replace the zero values in my cosumption column with THE HOURLY AVERAGE data
df[‘consumption’].replace(0, df_corrected.groupby(by=’hour’)[‘consumption’].mean())
Biswas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.