Skip to content

Instantly share code, notes, and snippets.

@kiview
Created April 1, 2021 11:25
Show Gist options
  • Save kiview/3f8fdf06f0c64dff85496f7e50ba11cc to your computer and use it in GitHub Desktop.
Save kiview/3f8fdf06f0c64dff85496f7e50ba11cc to your computer and use it in GitHub Desktop.
Pandas DataFrame Group by Consecutive Certain Values
# Taken from https://towardsdatascience.com/pandas-dataframe-group-by-consecutive-certain-values-a6ed8e5d8cc
df_indexed = df.reset_index(drop=True)
for k, v in df_indexed[df_indexed['behavior'] == 'not_defined'].groupby((df_indexed['behavior'] != 'not_defined').cumsum()):
print(f'[group {k}]')
display(v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment