Linked Questions

Popular Questions

Determine if two types appear within a timeframe, after groupby

Asked by At

I have a dataset where each row represents an event, with a date and a few pieces of information about the event. I would like to know whether, after grouping by one column (a), multiple types of another column (b) appear within a short timeframe (say, +/- 60 days), and keep rows where this is the case.

With a pandas dataframe that looks like:

     a      b          date
    foo    blue     2018-02-17
    foo    blue     2018-02-22
    foo    red      2018-04-28
    foo    blue     2018-04-29
    foo    blue     2018-05-02
    foo    red      2018-08-01
    bar    yellow   2018-01-25
    bar    red      2018-04-07
    bar    yellow   2018-07-11
    bar    yellow   2018-07-14
    baz    red      2018-03-11
    baz    blue     2018-04-14
    baz    red      2018-07-05
    baz    blue     2018-10-01

I would like to select the following rows:

     a      b          date
    foo    red      2018-04-28
    foo    blue     2018-04-29
    foo    blue     2018-05-02
    baz    red      2018-03-11
    baz    blue     2018-04-14

Related Questions