Find Null values and then fill Null with Mean Values

 

Find Null values and then fill Null with Mean Values
 

import pandas as pd

from sklearn.datasets import load_iris

# Load the Iris dataset

iris = load_iris()

# Convert the dataset to a DataFrame

df = pd.DataFrame(data=iris.data, columns=iris.feature_names)

# Apply isnull function

print("Null values before filling:")

print(df.isnull().sum())

# Fill null values with mean

df.fillna(df.mean(), inplace=True)

# Check if null values are filled with mean

print("\nNull values after filling with mean:")

df.isnull().sum()

 

Explanation:

Importing Libraries:

import pandas as pd: Imports the pandas library and assigns it the alias pd. Pandas is a powerful library for data manipulation and analysis in Python.

from sklearn.datasets import load_iris: Imports the load_iris function from the sklearn.datasets module. This function is used to load the Iris dataset, a popular dataset in machine learning.

load_iris(): Loads the Iris dataset into the variable iris. This dataset contains information about iris flowers, including sepal and petal dimensions, and species labels.

Converting to DataFrame:

pd.DataFrame(): Converts the data loaded from the Iris dataset into a pandas DataFrame.

data=iris.data: Passes the data (features) of the Iris dataset to create the DataFrame.

columns=iris.feature_names: Assigns column names to the DataFrame using the feature names from the Iris dataset.

Checking for Null Values:

df.isnull().sum(): Uses the isnull() function to identify missing values in the DataFrame. The .sum() function then counts the number of missing values in each column.

print("Null values before filling:"): Prints a message to indicate that the following output shows null values before filling.

Filling Null Values with Mean:

df.mean(): Calculates the mean of each column in the DataFrame.

df.fillna(): Fills missing values (NaN) in the DataFrame with the corresponding column means.

inplace=True: Modifies the DataFrame in place, meaning the changes are applied directly to the DataFrame df.

Checking Null Values After Filling:

print("\nNull values after filling with mean:"): Prints a message to indicate that the following output shows null values after filling with the mean.

df.isnull().sum(): Checks again for missing values in the DataFrame after filling them with the mean and prints the count of null values in each column.

 

टिप्पणी पोस्ट करा

0 टिप्पण्या