1

I have a Data Frame something like this.

+-----------------+-------+
|   Time Stamp    | Value |
+-----------------+-------+
| 11/18/2020 6:06 |     2 |
| 11/18/2020 6:06 |    19 |
| 11/18/2020 6:06 |     1 |
| 11/18/2020 6:06 |    21 |
| 11/18/2020 6:06 |     2 |
| 11/18/2020 6:07 |    52 |
| 11/18/2020 6:07 |     0 |
| 11/18/2020 6:08 |    20 |
| 11/18/2020 6:08 |     2 |
| 11/18/2020 6:08 |    20 |
| 11/18/2020 6:08 |     1 |
| 11/18/2020 6:08 |    19 |
| 11/18/2020 6:08 |     1 |
| 11/18/2020 6:09 |    20 |
| 11/18/2020 6:09 |     1 |
+-----------------+-------+

I want to add a new column to the Data Frame that contains the average of 4 consecutive values (each repeated 4 times) like this.

+-----------------+-------+------+
|   Time Stamp    | Value | Avg  |
+-----------------+-------+------+
| 11/18/2020 6:06 |     2 | 10.7 |
| 11/18/2020 6:06 |    19 | 10.7 |
| 11/18/2020 6:06 |     1 | 10.7 |
| 11/18/2020 6:06 |    21 | 10.7 |
| 11/18/2020 6:06 |     2 | 18.5 |
| 11/18/2020 6:07 |    52 | 18.5 |
| 11/18/2020 6:07 |     0 | 18.5 |
| 11/18/2020 6:08 |    20 | 18.5 |
| 11/18/2020 6:08 |     2 | 10.5 |
| 11/18/2020 6:08 |    20 | 10.5 |
| 11/18/2020 6:08 |     1 | 10.5 |
| 11/18/2020 6:08 |    19 | 10.5 |
| 11/18/2020 6:08 |     1 |  7.3 |
| 11/18/2020 6:09 |    20 |  7.3 |
| 11/18/2020 6:09 |     1 |  7.3 |
+-----------------+-------+------+

What is the easiest way to achieve this?

I have tried something like the answer in this link. But I couldn't Figure out how to add the average column back to the same Data Frame.

Arjun Ariyil
  • 386
  • 3
  • 14

0 Answers0