I have a dataframe which looks like this:
data1 = [['2020-10-01', '07-08', 3.0 ], ['2020-10-01', '08-09', 2.0], ['2020-10-01', '07-08', 3.0], ['2020-10-01', '07-08', 3.0],['2020-10-02', '07-08', 3.0 ], ['2020-10-02', '08-09', 3.0], ['2020-10-02', '07-08', 3.0], ['2020-10-02', '08-09', 3.0], ['2020-10-03', '09-10', 9.0], ['2020-10-03', '09-10', 9.0]]
df1 = pd.DataFrame(data1, columns = ['Date', 'TimeCategory', 'Value_TimeCategory_total'])
| Date | TimeCategory | Value_TimeCategory_total |
|---|---|---|
| 2020-10-01 | 07-08 | 3.0 |
| 2020-10-01 | 08-09 | 2.0 |
| 2020-10-01 | 07-08 | 3.0 |
| 2020-10-01 | 07-08 | 3.0 |
| 2020-10-02 | 07-08 | 3.0 |
| 2020-10-02 | 08-09 | 3.0 |
| 2020-10-02 | 07-08 | 3.0 |
| 2020-10-02 | 08-09 | 3.0 |
| 2020-10-03 | 09-10 | 9.0 |
| 2020-10-03 | 09-10 | 9.0 |
The Dataframe contains the total values for each TimeCategory during one day.
Now I would like to add a column to this dataframe which displays the average value for each TimeCategory during each day.
If I have 3 rows for with date 2020-10-01 and with the TimeCategory 07-08 and the total value equals 3.0, I would like to have the average value equals 1.0.
The result should look like this.
data2 = [['2020-10-01', '07-08', 3.0 , 1.0], ['2020-10-01', '08-09', 2.0, 2.0], ['2020-10-01', '07-08', 3.0, 1.0], ['2020-10-01', '07-08', 3.0, 1.0],['2020-10-02', '07-08', 3.0, 1.5 ], ['2020-10-02', '08-09', 3.0, 1.5], ['2020-10-02', '07-08', 3.0, 1.5], ['2020-10-02', '08-09', 3.0, 1.5], ['2020-10-03', '09-10', 9.0, 4.5], ['2020-10-03', '09-10', 9.0, 4.5]]
df2 = pd.DataFrame(data2, columns = ['Date', 'TimeCategory', 'Value_TimeCategory_total' , 'Value_TimeCategory_Row_Average'])
df2
| Date | TimeCategory | Value_TimeCategory_total | Value_TimeCategory_Row_Average |
|---|---|---|---|
| 2020-10-01 | 07-08 | 3.0 | 1.0 |
| 2020-10-01 | 08-09 | 2.0 | 2.0 |
| 2020-10-01 | 07-08 | 3.0 | 1.0 |
| 2020-10-01 | 07-08 | 3.0 | 1.0 |
| 2020-10-02 | 07-08 | 3.0 | 1.5 |
| 2020-10-02 | 08-09 | 3.0 | 1.5 |
| 2020-10-02 | 07-08 | 3.0 | 1.5 |
| 2020-10-02 | 08-09 | 3.0 | 1.5 |
| 2020-10-03 | 09-10 | 9.0 | 4.5 |
| 2020-10-03 | 09-10 | 9.0 | 4.5 |
I do not want to use group by, because I need all rows (including duplicates) of my dataframe.
Many thanks in advance for your help.