I'm looking for an efficient way to create the 'average_units' column - which, for each group in Col B, goes row by row and computes the average units for previous 4 days while EXCLUDING 0 unit days. Simple example below:
Original DF
ColA ColB Units
2021-01-01 A 2
2021-01-02 A 0
2021-01-03 A 4
2021-01-04 A 8
2021-01-05 A 1
2021-01-06 A 0
2021-01-07 A 0
2021-01-08 A 7
2021-01-01 B 3
2021-01-02 B 6
2021-01-03 B 9
2021-01-04 B 0
2021-01-05 B 1
2021-01-06 B 7
2021-01-07 B 0
2021-01-08 B 5
Desired DF... Example of 'Avg Units' calculation: On 2021-01-08 in group A... calculation will look at the previous 4 days of units (0,0,1,8)...and will compute the average with the 0's excluded - thus 1+8 / 2 = 4.5
ColA ColB Units Avg Units
2021-01-01 A 2 NA
2021-01-02 A 0 NA
2021-01-03 A 4 NA
2021-01-04 A 8 NA
2021-01-05 A 1 7
2021-01-06 A 0 6.5
2021-01-07 A 0 6.5
2021-01-08 A 7 4.5
2021-01-01 B 3 NA
2021-01-02 B 6 NA
2021-01-03 B 9 NA
2021-01-04 B 0 NA
2021-01-05 B 1 9
2021-01-06 B 7 8
2021-01-07 B 0 8.5
2021-01-08 B 5 4
I know this can be done with some loops but it would be super complex and time consuming because I have 1000's of groups and more dates. Any suggestions is appreciated!