Calculate the most recent two payments per customer annually then take an average

ID       2016   2017   2018    2019       
25471    $50     $0     $10    $400
22547    $0      $50    $25    $0
38754    $0     $1000   $0     $50
14523    $100   $500    $300   $0
14789    $400   $400    $400   $400 

I would like to know how to find the two most recent payments for a customer, then subtract one from another and divide. For example for ID 22547, 2017 and 2018 are the two most recent payments. I'd like take (2018-2017/2018). I'm still very new at python and learning on my own. Any help would be appreciated. I tried to do an average between columns but that does not give me the right output. Thanks in advance



Read more here: https://stackoverflow.com/questions/66995240/calculate-the-most-recent-two-payments-per-customer-annually-then-take-an-averag

Content Attribution

This content was originally published by K Bard at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: