Search code examples
pythonpandasnumpypandas-groupbydata-analysis

Subtracting Rows based on ID Column - Pandas


I have a dataframe which looks like this:

UserId    Date_watched    Days_not_watch
  1        2010-09-11         5
  1        2010-10-01         8
  1        2010-10-28         1
  2        2010-05-06         12
  2        2010-05-18         5
  3        2010-08-09         10
  3        2010-09-25         5

I want to find out the no. of days the user gave as a gap, so I want a column for each row for each user and my dataframe should look something like this:

UserId    Date_watched    Days_not_watch      Gap(2nd watch_date - 1st watch_date - days_not_watch)
  1        2010-09-11         5                0   (First gap will be 0 for all users)
  1        2010-10-01         8                15 (11th Sept+5=16th Sept; 1st Oct - 16th Sept=15days)
  1        2010-10-28         1                9
  2        2010-05-06         12               0
  2        2010-05-18         5                0 (because 6th May+12 days=18th May)
  3        2010-08-09         10               0
  3        2010-09-25         4                36
  3        2010-10-01         2                2

I have mentioned the formula for calculating the Gap beside the column name of the dataframe.


Solution

  • Here is one approach using groupby + shift:

    # sort by date first
    df['Date_watched'] = pd.to_datetime(df['Date_watched'])
    df = df.sort_values(['UserId', 'Date_watched'])
    
    # calculate groupwise start dates, shifted
    grp = df.groupby('UserId')
    starts = grp['Date_watched'].shift() + \
             pd.to_timedelta(grp['Days_not_watch'].shift(), unit='d')
    
    # calculate timedelta gaps
    df['Gap'] = (df['Date_watched'] - starts).fillna(pd.Timedelta(0))
    
    # convert to days and then integers
    df['Gap'] = (df['Gap'] / pd.Timedelta('1 day')).astype(int)
    
    print(df)
    
       UserId Date_watched  Days_not_watch  Gap
    0       1   2010-09-11               5    0
    1       1   2010-10-01               8   15
    2       1   2010-10-28               1   19
    3       2   2010-05-06              12    0
    4       2   2010-05-18               5    0
    5       3   2010-08-09              10    0
    6       3   2010-09-25               5   37