Pandas dataframe sort and groupby columns and add columns that are based on calclations from previous group

25 Views Asked by At

I have a df:

df = Date                id1  amount is_winner
2022-07-14 02:34:20.348.  A.    87.11. False
2022-07-14 02:34:20.348.  B.    77.12. True
2022-07-14 02:37:20.348.  A     89.11. False
2022-07-14 02:37:20.348.  B.    87.12. True
2022-07-14 02:44:20.348.  A.    81.11. True
2022-07-14 02:44:20.348.  B.    87.12. False
2022-07-14 02:54:20.348.  A.    99.11. False
2022-07-14 02:54:20.348.  B.    99.11. True

Now, I want to apply groupby(["date", "id1"]) and have various measures that are based on previous groups (notice data is sorted by date):

df = 
Date                      id1  amount is_winner delta_amount_from_last_win  num_wins_before. num_groups_before. rank_in_prev_group. time_from_prev(minutes)
2022-07-14 02:34:20.348.  A.    87.11. False.            0.                          0.             0.                 nan.                 nan
2022-07-14 02:34:20.348.  B.    77.12. True              0.                          0.             0.                 nan.                 nan
2022-07-14 02:37:20.348.  A     89.11. False.            2.                          0.             1.                   1.                   3
2022-07-14 02:37:20.348.  B.    87.12. True.             10                          1.             1.                    0.                  3
2022-07-14 02:44:20.348.  A.    81.11. True.             -8.                         0.             2.                    1.                  7
2022-07-14 02:44:20.348.  B.    87.12. False.             0.                         1.             2.                   0.                   7
2022-07-14 02:54:20.348.  A.    99.11. False.            18.                         1              3.                   0.                   10
2022-07-14 02:54:20.348.  B.    99.12. True.             12                          2.             3.                    1.                  10

How can it be done efficiently?

0

There are 0 best solutions below