I defined the following formula to calculate the Net Promoter Score ("NPS"):
def nps_calc(data):
promoter_share = data[data.nps > 8].count()/len(data)
detractor_share = data[data.nps < 7].count()/len(data)
nps_score = (round((promoter_share - detractor_share)*100,0))
return nps_score
I have applied the above formula in a groupby statement to calculate NPS scores for individual months based on the "nps" column. The "month" column is simply an integer (1-12).
df.groupby(['Country','Segment','Month']).apply(nps_calc).nps
What I am stuck with is to go from this single month perspective into a 3-months rolling perspective. That means, apply the NPS formula over a 3-months rolling window per month and derive NPS from the percentage share of all "promoters" from the last 3 months minus the percentage share of all "detractors" from the last 3 months.
Please note: I'm not talking about moving averages here (averaging 3 individual months scores is not the same and results vary slightly).
I am a bit lost between statements like ".rolling()", ".apply()", the latest Pandas release (with some improvements in rolling calculations?) and maybe another custom definition for rolling NPS calculation?
What do you think is the best way to tackle this?
Here is how the input data looks like. The table is quite big but these are the variables of interest. There are multiple countries, segments and months from 2020. NPS ratings are integers from 0-10.
| Country | Segment | Month | nps |
| 1 | 1 | 1 | 7 |
| 1 | 1 | 1 | 4 |
| 1 | 1 | 1 | 9 |
| 1 | 2 | 1 | 10 |
| 1 | 2 | 2 | 7 |
| 1 | 2 | 2 | 5 |
| 1 | 1 | 3 | 1 |
| 1 | 2 | 3 | 9 |
| 1 | 2 | 1 | 6 |
| 2 | 2 | 2 | 8 |
| 2 | 1 | 3 | 0 |