Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dataframe get missing and zero values

def describe_missing_zeros_values(df: pd.core.frame.DataFrame) -> pd.core.frame.DataFrame:
    """Describe Missing and Zero Valued columns in a dataframe.
    
    Args:
        df (pd.core.frame.DataFrame): Dataframe under analysis.
        
    Returns:
        pd.core.frame.Dateframe: Dataframe with missing and zero valued columns and their statistics.
    """
    zero_values = (df == 0.00).astype(int).sum(axis=0)
    missing_values = df.isnull().sum()
    missing_values_percent = missing_values * 100 / len(df)
    missing_zero_df = pd.concat([zero_values, missing_values, missing_values_percent], axis=1)
    missing_zero_df = missing_zero_df.rename(
                            columns = {0 : "Zero Values", 1 : "Missing Values", 2 : "% Missing Values"})
    missing_zero_df["Total Zero & Missing Values"] = missing_zero_df["Zero Values"] + missing_zero_df["Missing Values"]
    missing_zero_df["% Total Zero & Missing Values"] = 100 * missing_zero_df["Total Zero & Missing Values"] / len(df)
    missing_zero_df["Data Type"] = df.dtypes
    missing_zero_df = missing_zero_df[missing_zero_df.iloc[:,1] != 0].sort_values("% Missing Values", ascending=False).round(1)
    print(f"Your selected dataframe has {df.shape[0]} rows {df.shape[1]} columns.")
    print(f"There are {missing_zero_df.shape[0]} columns that have missing values.")
    return missing_zero_df
Comment

PREVIOUS NEXT
Code Example
Python :: python regex with f-string 
Python :: for y in range(10): for x in range(y): print("*",end=') print() 
Python :: replace dataframe column element if element is within a specific list 
Python :: patterns and matcher nfa python code 
Python :: discord.py main file setup 
Python :: sns.kdeplot make line more detailed 
Python :: ring Copy Lists 
Python :: ring write the same example using normal for loop the Encrypt() and Decrypt() functions. 
Python :: store image in django postprocessimage in django storage 
Python :: nnumpy matrix count non negative values 
Python :: gfxdraw circle weight 
Python :: plot a list of number in python 
Python :: void setup and void loop 
Python :: module not imorting idle 
Python :: read past tense 
Python :: global variable not accessible withing thread 
Python :: how to split string into list conditionally+python 
Python :: python apply file line 
Python :: run django using nssm 
Python :: Capitalize first word of a phrase in python 
Python :: how to change text in a canvas tkinter 
Python :: how can space be bent 
Python :: gizeh python 
Python :: sleep python 
Python :: aw mustard 
Python :: create graph, x y axis | graph plotting 
Python :: add js file in web.assets_backend 
Python :: Incrémenter/décrémenter variable python 
Python :: import all files on the same directory python 
Python :: write python code in ansible 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =