Skip to content

Instantly share code, notes, and snippets.

@Ailuropoda1864
Last active September 6, 2017 19:06
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 Ailuropoda1864/46761b8f20669afa4871942ee444220a to your computer and use it in GitHub Desktop.
Save Ailuropoda1864/46761b8f20669afa4871942ee444220a to your computer and use it in GitHub Desktop.
prints value counts for each (categorical) column
import pandas as pd
from pandas.core.dtypes.common import (
is_numeric_dtype, is_datetime64_dtype, is_bool_dtype
)
def category_counts(dataframe, max_nunique=20, numeric=False, datetime=False):
"""
prints value counts for each (categorical) column
:param dataframe: a pandas DataFrame
:param max_nunique: the max number of unique values a column can have for
its value counts to be printed; no limit is set if None
:param numeric: boolean; if True, value counts for numeric data are also
printed
:param datetime: boolean; if True, value counts for datetime data are also
printed
:return: None
"""
for column in dataframe.columns:
col = dataframe[column]
if is_bool_dtype(col):
print(col.value_counts())
print('\n')
break
if not any([
max_nunique is not None and col.nunique() > max_nunique,
not numeric and is_numeric_dtype(col),
not datetime and is_datetime64_dtype(col)
]):
print(col.value_counts())
print('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment