Skip to content

Instantly share code, notes, and snippets.

@Ailuropoda1864
Last active September 6, 2017 04:47
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/45c14c79551220d959b1a6b210f7e7f5 to your computer and use it in GitHub Desktop.
Save Ailuropoda1864/45c14c79551220d959b1a6b210f7e7f5 to your computer and use it in GitHub Desktop.
Customization of the describe method for pandas dataframe; prints descriptions of all columns (grouped by numeric, datetime, boolean, and others) and DatetimeIndex (if any)
import numpy as np
import pandas as pd
from pandas.core.dtypes.common import (
is_numeric_dtype, is_datetime64_dtype, is_bool_dtype
)
from pandas.core.indexes.datetimes import DatetimeIndex
def describe_by_type(dataframe):
"""
prints descriptions of all columns (grouped by numeric, datetime, boolean,
and others) and DatetimeIndex (if any)
:param dataframe: a pandas DataFrame
:return: None
"""
boolean, numeric, datetime, other = False, False, False, False
for column in dataframe.columns:
if is_bool_dtype(dataframe[column]):
boolean = True
elif is_numeric_dtype(dataframe[column]):
numeric = True
elif is_datetime64_dtype(dataframe[column]):
datetime = True
else:
other = True
# describe datetime columns and DatetimeIndex (if any)
if isinstance(dataframe.index, DatetimeIndex):
print(pd.Series(dataframe.index).describe())
print('\n')
if datetime:
print(dataframe.describe(include=['datetime']))
print('\n')
# describe numeric columns (if any)
if numeric:
print(dataframe.describe())
print('\n')
# describe boolean columns (if any)
if boolean:
print(dataframe.describe(include=[np.bool]))
print('\n')
# describe other columns (if any)
if other:
print(dataframe.describe(exclude=[np.number, np.datetime64, np.bool]))
print('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment