Skip to content

Instantly share code, notes, and snippets.

@tweddielin
Created September 11, 2020 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tweddielin/9f2bc0be768f76fb55eeb7db801bcbe3 to your computer and use it in GitHub Desktop.
Save tweddielin/9f2bc0be768f76fb55eeb7db801bcbe3 to your computer and use it in GitHub Desktop.
def graph_approach(df, start='start', end='end'):
start = df[start].values
end = df[end].values
graph = (start <= end[:, None]) & (end >= start[:, None])
n_components, indices = connected_components(graph, directed=False)
return df.groupby(indices).aggregate({'start': 'min','end': 'max'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment