Skip to content

Instantly share code, notes, and snippets.

@nhatbui
Last active November 20, 2015 05:55
Show Gist options
  • Save nhatbui/8dff90e610cdc47cfc68 to your computer and use it in GitHub Desktop.
Save nhatbui/8dff90e610cdc47cfc68 to your computer and use it in GitHub Desktop.
Count Unique from Pandas DataFrame
import pandas as pd
d = {
'a': ['1','1','2','2','2','3'],
'b': ['green', 'blue','yellow','yellow','blue','green']
}
df = pd.DataFrame.from_dict(d)
df_count = pd.crosstab(df['a'], df['b'])
@nhatbui
Copy link
Author

nhatbui commented Nov 20, 2015

Motivation: Is there a Pandas-only way to take a DataFrame, group by a column, and count all unique values of another column?

>>> df
   a       b
0  1   green
1  1    blue
2  2  yellow
3  2  yellow
4  2    blue
5  3   green
>>> df_count = some_process(df)
>>> df_count
   blue  green  yellow
1     1      1       0
2     1      0       2
3     0      1       0

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