Skip to content

Instantly share code, notes, and snippets.

@msure
Last active August 26, 2020 19:41
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 msure/3bbaf49bba7dd016dce77f785ad43a18 to your computer and use it in GitHub Desktop.
Save msure/3bbaf49bba7dd016dce77f785ad43a18 to your computer and use it in GitHub Desktop.
Pandas Scratchpad
# faster/vectorized replacement for getting monthly cohort
df['months_elapsed'] = (df['stats_date'].dt.month - df['first_date'].dt.month) + (12 * (df['stats_date'].dt.year - df['first_date'].dt.year))
# for handy functions (pandas, seaborn, etc.) where you need to reshape the dataframe first
df.query('some_filter == "2020-01-01" & another_filter != "error"').pipe(lambda x: pd.crosstab(x['variation'],x['customers']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment