Skip to content

Instantly share code, notes, and snippets.

@gjreda
Last active August 2, 2016 14:51
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 gjreda/320106138841808f5c3857124f2acf82 to your computer and use it in GitHub Desktop.
Save gjreda/320106138841808f5c3857124f2acf82 to your computer and use it in GitHub Desktop.
days since last login -- pandas groupby cumulative count with reset
# for creating a column like "days since last login"
df = pd.read_clipboard(index_col=['customer_id', 'days'])
(df
.groupby(level='customer_id')
.did_login
.cumsum()
.to_frame()
.groupby(level='customer_id')
.apply(lambda g: g.groupby('did_login').cumcount())
)
@gjreda
Copy link
Author

gjreda commented Apr 20, 2016

Input:

customer_id days    did_login
2   -10 1
2   -9  1
2   -8  1
2   -7  1
2   -6  0
2   -5  0
2   -4  1
2   -3  0
2   -2  0
2   -1  0
2   0   1
8   -10 0
8   -9  1
8   -8  1
8   -7  1
8   -6  1
8   -5  0
8   -4  1
8   -3  1
8   -2  0
8   -1  0
8   0   0

Output:

2   2   -10 0
2   2   -9  0
2   2   -8  0
2   2   -7  0
2   2   -6  1
2   2   -5  2
2   2   -4  0
2   2   -3  1
2   2   -2  2
2   2   -1  3
2   2   0   0
8   8   -10 0
8   8   -9  0
8   8   -8  0
8   8   -7  0
8   8   -6  0
8   8   -5  1
8   8   -4  0
8   8   -3  0
8   8   -2  1
8   8   -1  2
8   8   0   3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment