I have a dataframe where i wish to compute the number of days (timedelta) that a unique asset remained installed. The sample input dataframe is as follows
df =pd.DataFrame({'Date': ['2007-11-01','2012-03-02','2012-03-02','2013-04-01','2013-04-01','2017-11-15','2017-11-15'], 'action':['installed','installed','removed','installed','removed','installed','removed'], 'asset_alphnum':['A-3724','A-3534','A-3724','A2732','A-3534','A-2007','A2732']})
Outputs:
I have tried pd.crosstab
pd.crosstab(df.asset_alphnum, [df.Date, df.action])
However I cannot work out how to take it from here. Somehow need to collapse the hierarchical index and subract one date from the next.
Any guidance much appreciated.