Last active
September 6, 2017 19:06
-
-
Save Ailuropoda1864/46761b8f20669afa4871942ee444220a to your computer and use it in GitHub Desktop.
prints value counts for each (categorical) column
This file contains 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
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