【问题标题】:Subtracting Rows based on ID Column - Pandas根据 ID 列减去行 - Pandas
【发布时间】:2019-01-23 11:25:41
【问题描述】:

我有一个如下所示的数据框:

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

我想知道号码。用户给出的天数作为间隔,所以我希望每个用户的每一行都有一列,我的数据框应该如下所示:

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

我在数据框的列名旁边提到了计算 Gap 的公式。

【问题讨论】:

    标签: python pandas numpy pandas-groupby data-analysis


    【解决方案1】:

    这是使用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
    

    【讨论】:

    • 有一个问题,我的日期没有排序,这里我按升序发布,但实际上没有排序,当我排序时,最终数据帧索引不匹配。我该如何解决?
    • @DebadriDutta,然后先按用户和日期排序,请参阅更新。我的解决方案不在任何地方使用数据帧索引。
    • 我把它整理好了。感谢您的回答,它工作正常:)
    猜你喜欢
    • 2021-01-06
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    相关资源
    最近更新 更多