I have a situation where I need to pivot the data in order to fetch the most recent and next to recent entry basis the date field. My data frame looks like:
State country Date confirmed deaths recover
A C 1/22/20 1 0 0
A C 1/23/20 1 0 0
A C 1/24/20 15 0 0
A C 1/25/20 39 0 0
B C 1/26/20 60 0 0
B C 1/27/20 70 0 0
B C 1/28/20 106 0 0
B C 1/29/20 152 2 0
B C 1/30/20 200 2 0
and the output I want should be like this: So new the columns confirmed, deaths and recover should fetch the values basis the Max(Date) which is 1/25/20 for State A and 1/30/20 for State B and newcases , newdeaths, newrecover should be the difference of the Max date values - second to max date values.
For example for state A new cases = 39-15= 24, newdeaths = 0-0=0 , newrecover = 0-0=0
39 cases were at the max date and 24 cases were at the second to the max date as I need to get the daily change. This should be dynamic as it needs to be fetched daily
State country confirmed deaths recover newcases newdeaths newrecover
A C 39 0 0 24 0 0
B C 200 2 0 48 2 0