0% found this document useful (0 votes)
14 views3 pages

Functions for Handling Missing Data

The document provides an overview of functions for detecting, removing, and imputing missing data in a DataFrame using pandas. It includes examples of each function, such as df.isnull(), df.dropna(), and df.fillna(), along with a sample program demonstrating the detection and handling of missing data. Additionally, it outlines analysis functions to summarize missing data statistics.

Uploaded by

freaktabla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Functions for Handling Missing Data

The document provides an overview of functions for detecting, removing, and imputing missing data in a DataFrame using pandas. It includes examples of each function, such as df.isnull(), df.dropna(), and df.fillna(), along with a sample program demonstrating the detection and handling of missing data. Additionally, it outlines analysis functions to summarize missing data statistics.

Uploaded by

freaktabla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Detection Functions (Finding Missing Data)

Function Description Example

Returns a boolean DataFrame (True where


[Link]() [Link]()
value is missing)

Opposite of isnull() (True where value is not


[Link]() [Link]()
missing)

[Link]() Same as isnull() [Link]()

[Link]() Same as notnull() [Link]()

[Link]().sum() Count of missing values per column [Link]().sum()

[Link]().sum().sum() Total missing values in entire DataFrame [Link]().sum().sum()

[Link]().mean() Percentage of missing values per column ([Link]().mean() * 100)

Displays all rows that have at least one missing


df[[Link]().any(axis=1)]
value

df[[Link]().all(axis=1)] Displays rows where all values are missing

🔹 2️⃣ Removal Functions (Dropping Missing Data)

Function Description Example

[Link]() Drop all rows with any missing values [Link]()

[Link](axis=1) Drop columns with any missing values [Link](axis=1)

[Link](how='all') Drop rows where all values are missing [Link](how='all')

[Link](subset=['col1', Drop rows if specified columns have missing


'col2']) values

Keep only rows with at least 3 non-null


[Link](thresh=3)
values

🔹 3️⃣ Imputation Functions (Filling Missing Data)

Function Description Example

Replace missing values with a specified


[Link](value) [Link](0)
constant

Replace missing numeric values with


[Link]([Link]())
column mean
Function Description Example

[Link]([Link]()) Replace with column median

df['col'].fillna(df['col'].mode()[0]) Replace with mode (for categorical data)

[Link](method='ffill') Forward fill (copy previous value)

[Link](method='bfill') Backward fill (copy next value)

Estimate missing values based on linear


[Link]()
trend

[Link]([Link], value) Replace NaN with a given value [Link]([Link], 0)

🔹 4️⃣ Analysis and Summary Functions

Function Description Example

[Link]() Displays non-null counts per column

[Link]() Summary stats — missing data appears as fewer counts

([Link]().sum() / len(df)) * 100 Percentage of missing values per column

[Link]().sum(axis=1) Number of missing values per row

[Link]().any(axis=1).sum() Number of rows having at least one missing value

Missing Data Example Program

# ---------------------------------------------

import pandas as pd

import numpy as np
# 1️⃣ Create a sample dataset

data = {

'Product': ['Laptop', 'Tablet', 'Mobile', 'Monitor', 'Keyboard'],

'Price': [60000, 30000, [Link], 15000, 2500],

'Quantity': [10, [Link], 25, 8, [Link]],

'Category': ['Electronics', 'Electronics', 'Electronics', [Link], 'Accessories']

df = [Link](data)

print("Original DataFrame:\n", df)

# 2️⃣ Detect missing data

print("\nMissing Values per Column:")

print([Link]().sum())

# 4️⃣ Handle missing data

# Fill numeric columns with mean

df['Price'].fillna(df['Price'].mean(), inplace=True)

df['Quantity'].fillna(df['Quantity'].median(), inplace=True)

# Fill categorical columns with mode

df['Category'].fillna(df['Category'].mode()[0], inplace=True)

# 5️⃣ Display cleaned data

print("\nCleaned DataFrame:\n", df)

# 6️⃣ Verify no missing values remain

print("\nMissing Values after Cleaning:")

print([Link]().sum())

You might also like