Skip to content

Instantly share code, notes, and snippets.

Created Sep 11, 2020
What would you like to do?
def graph_approach(df, key='person_id', start='start', end='end'):
start = df[start]values
end = df[end]values
person_id = df[person_id]values
graph = (start <= end[:, None]) & (end >= start[:, None]) & (person_id == person_id[:, None])
n_components, indices = connected_components(graph, directed=False)
return df.groupby(indices).aggregate({'person_id': 'first', 'start': 'min','end': 'max'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment