-
-
Save tweddielin/9a1b1c560dd4c803fd34aadf5f4c3e7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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