Skip to content

Instantly share code, notes, and snippets.

@edvardm
Last active March 12, 2023 18:50
Show Gist options
  • Save edvardm/485241adda07fb7da54dddfc9e8111e6 to your computer and use it in GitHub Desktop.
Save edvardm/485241adda07fb7da54dddfc9e8111e6 to your computer and use it in GitHub Desktop.
more informative describe()
# Credits to answer in https://stackoverflow.com/questions/53173927/pandas-extensive-describe-include-count-the-null-values
def stats(df: pd.DataFrame) -> pd.DataFrame:
"""Like describe() but return dataframe, with datatypes and ratio of NaN values"""
st = df.describe()
total_n = len(df)
st.loc["dtype"] = df.dtypes
st.loc["n"] = total_n
st.loc["nan_n"] = total_n - df.count() # number of NaNs
st.loc["nan_r"] = df.isnull().mean() # ratio NaN/total length
return st
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment