1

I have a pandas df:

pd.DataFrame({'team': ['ARI','ARI','ARI','BAL','BAL','BAL','BUF','BUF','BUF'],
          'year': [2019, 2019, 2020, 2019, 2019, 2020, 2019, 2019, 2020],
          'week': [16,17,1,16,17,1,16,17,1],
          'hits': [1, 3, 2, 2, 2, 5, 3, 3, 2]})

I want to overwrite the "hits" column to a cumulative sum, but the cumsum must reset every "year". Expected output:

pd.DataFrame({'team': ['ARI','ARI','ARI','BAL','BAL','BAL','BUF','BUF','BUF'],
          'year': [2019, 2019, 2020, 2019, 2019, 2020, 2019, 2019, 2020],
          'week': [16,17,1,16,17,1,16,17,1],
          'cumsum_hits': [1,4,2,2,4,5,3,6,2]})

0 Answers0