Last active
August 2, 2016 14:51
-
-
Save gjreda/320106138841808f5c3857124f2acf82 to your computer and use it in GitHub Desktop.
days since last login -- pandas groupby cumulative count with reset
This file contains hidden or 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
# 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()) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Input:
Output: