'Grouping the data in python
Suppose, I have the data like this,
Date Time Energy_produced
01.01.2016 00:00 500
01.01.2016 00:15 580
01.01.2016 00:30 600
01.01.2016 00:45 620
01.01.2016 01:00 580
01.01.2016 01:15 520
01.01.2016 01:30 590
01.01.2016 01:45 570
01.01.2016 02:00 540
Now, i want to sum the energy produced based on each hour
suppose ,
Date Hour Energy produced per hour
01.01.2016 00:00 2280(per hour)
01:01:2016 01:00 2240(per hour)
How to sum like this?
Solution 1:[1]
If you want to keep Date/Time as strings, you could use:
(df.groupby(['Date', df['Time'].str[:3].rename('Hour')+'00'])
['Energy_produced'].sum()
.reset_index()
)
Output:
Date Hour Energy_produced
0 01.01.2016 00:00 2300
1 01.01.2016 01:00 2260
2 01.01.2016 02:00 540
NB. You can also get the second group with: df['Time'].str.replace(r'\d{2}$', '00', regex=True).rename('Hour')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mozway |
