Quantcast
Channel: Active questions tagged row - Stack Overflow
Viewing all articles
Browse latest Browse all 480

How to add new rows to Pandas data frame based on calculation using all rows for a given datetime?

$
0
0

I have the following data frame that consists of device electricity usage in my home broken up by hour.

             DateTime                   Name    kWh0     1/31/2024 18:00              Sump Pump  0.0031     1/31/2024 18:00              Furnace 2  0.0272     1/31/2024 18:00              Furnace 1  0.2113     1/31/2024 18:00                  Dryer  0.3024     1/31/2024 18:00            Total Usage  1.9275     1/31/2024 18:00              Always On  0.8116     1/31/2024 18:00                 Heat 7  0.0037     1/31/2024 18:00                 Fridge  0.0308     1/31/2024 18:00                   Pump  0.0699     1/31/2024 19:00              Sump Pump  0.00210    1/31/2024 19:00            Total Usage  1.59311    1/31/2024 19:00              Always On  0.82812    1/31/2024 19:00                 Heat 7  0.00113    1/31/2024 19:00                 Fridge  0.0214    1/31/2024 19:00                   Pump  0.070

I would like to add a row for every hour where Name = "Other" and kWh equals ("Total Usage" kWh - the sum of kWh for all other rows for that hour) so the resulting data frame would be the following. The Names, order, and number of rows for each hour will vary. (Note I added 2 rows where Name = "Other".

             DateTime                   Name    kWh0     1/31/2024 18:00              Sump Pump  0.0031     1/31/2024 18:00              Furnace 2  0.0272     1/31/2024 18:00              Furnace 1  0.2113     1/31/2024 18:00                  Dryer  0.3024     1/31/2024 18:00            Total Usage  1.9275     1/31/2024 18:00              Always On  0.8116     1/31/2024 18:00                 Heat 7  0.0037     1/31/2024 18:00                 Fridge  0.0308     1/31/2024 18:00                   Pump  0.0699     1/31/2024 18:00                  Other  0.47110    1/31/2024 19:00              Sump Pump  0.00211    1/31/2024 19:00            Total Usage  1.59312    1/31/2024 19:00              Always On  0.82813    1/31/2024 19:00                 Heat 7  0.00114    1/31/2024 19:00                 Fridge  0.0215    1/31/2024 19:00                   Pump  0.07016    1/31/2024 19:00                  Other  0.672

Thanks in advance for your help!


Viewing all articles
Browse latest Browse all 480

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>