Skip to content

Instantly share code, notes, and snippets.

@dumbledad
Last active July 15, 2019 16:40
Show Gist options
  • Save dumbledad/0cf900c0cff3366807436cbbb81c64b1 to your computer and use it in GitHub Desktop.
Save dumbledad/0cf900c0cff3366807436cbbb81c64b1 to your computer and use it in GitHub Desktop.
Why does nunique overwrite the grouping column in pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'user_id':[1,1,1,2,2,2],'module_id':[1,2,3,1,1,2]})
print(df)
print(df.groupby('user_id', as_index=False).sum())
print(df.groupby('user_id', as_index=False).nunique())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment