Skip to content Skip to sidebar Skip to footer

Count Overlapping Time Frames In A Pandas Dataframe, Grouped By Person

I'm using the top solution here to determine the number of rows that have start and end times overlapping with the given row. However, I need these overlaps to be determined by gro

Solution 1:

I think this might give what you need.

Add in an extra & condition for matching on name too:

ends = df['start_time'].values < df['end_time'].values[:, None]
starts = df['start_time'].values > df['start_time'].values[:, None]
same_group = (df['name'].values == df['name'].values[:, None])

# sum across axis=1 !!!
df['overlap'] = (ends & starts & same_group).sum(1)

df

Post a Comment for "Count Overlapping Time Frames In A Pandas Dataframe, Grouped By Person"