Handle missing values in the Iris dataset using different methods. (Dataset: Iris built-in)¶

In [1]:
!pip install numpy
!pip install pandas
!pip install scikit-learn
Requirement already satisfied: numpy in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (2.3.2)
Requirement already satisfied: pandas in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (2.3.1)
Requirement already satisfied: numpy>=1.26.0 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from pandas) (2.3.2)
Requirement already satisfied: python-dateutil>=2.8.2 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from pandas) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from pandas) (2025.2)
Requirement already satisfied: tzdata>=2022.7 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from pandas) (2025.2)
Requirement already satisfied: six>=1.5 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from python-dateutil>=2.8.2->pandas) (1.17.0)
Requirement already satisfied: scikit-learn in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (1.7.1)
Requirement already satisfied: numpy>=1.22.0 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from scikit-learn) (2.3.2)
Requirement already satisfied: scipy>=1.8.0 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from scikit-learn) (1.16.1)
Requirement already satisfied: joblib>=1.2.0 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from scikit-learn) (1.5.1)
Requirement already satisfied: threadpoolctl>=3.1.0 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from scikit-learn) (3.6.0)
In [2]:
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
In [3]:
data = load_iris()
df = pd.DataFrame(data.data, columns = data.feature_names)
In [4]:
df.head()
Out[4]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
In [5]:
df.tail()
Out[5]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
145 6.7 3.0 5.2 2.3
146 6.3 2.5 5.0 1.9
147 6.5 3.0 5.2 2.0
148 6.2 3.4 5.4 2.3
149 5.9 3.0 5.1 1.8
In [6]:
data.feature_names
Out[6]:
['sepal length (cm)',
 'sepal width (cm)',
 'petal length (cm)',
 'petal width (cm)']
In [7]:
print(data.data)
[[5.1 3.5 1.4 0.2]
 [4.9 3.  1.4 0.2]
 [4.7 3.2 1.3 0.2]
 [4.6 3.1 1.5 0.2]
 [5.  3.6 1.4 0.2]
 [5.4 3.9 1.7 0.4]
 [4.6 3.4 1.4 0.3]
 [5.  3.4 1.5 0.2]
 [4.4 2.9 1.4 0.2]
 [4.9 3.1 1.5 0.1]
 [5.4 3.7 1.5 0.2]
 [4.8 3.4 1.6 0.2]
 [4.8 3.  1.4 0.1]
 [4.3 3.  1.1 0.1]
 [5.8 4.  1.2 0.2]
 [5.7 4.4 1.5 0.4]
 [5.4 3.9 1.3 0.4]
 [5.1 3.5 1.4 0.3]
 [5.7 3.8 1.7 0.3]
 [5.1 3.8 1.5 0.3]
 [5.4 3.4 1.7 0.2]
 [5.1 3.7 1.5 0.4]
 [4.6 3.6 1.  0.2]
 [5.1 3.3 1.7 0.5]
 [4.8 3.4 1.9 0.2]
 [5.  3.  1.6 0.2]
 [5.  3.4 1.6 0.4]
 [5.2 3.5 1.5 0.2]
 [5.2 3.4 1.4 0.2]
 [4.7 3.2 1.6 0.2]
 [4.8 3.1 1.6 0.2]
 [5.4 3.4 1.5 0.4]
 [5.2 4.1 1.5 0.1]
 [5.5 4.2 1.4 0.2]
 [4.9 3.1 1.5 0.2]
 [5.  3.2 1.2 0.2]
 [5.5 3.5 1.3 0.2]
 [4.9 3.6 1.4 0.1]
 [4.4 3.  1.3 0.2]
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.9 3.  5.1 1.8]]
In [8]:
df.iloc[::10] = np.nan
df.isnull().sum()
Out[8]:
sepal length (cm)    15
sepal width (cm)     15
petal length (cm)    15
petal width (cm)     15
dtype: int64
In [9]:
df.head(20)
Out[9]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 NaN NaN NaN NaN
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
5 5.4 3.9 1.7 0.4
6 4.6 3.4 1.4 0.3
7 5.0 3.4 1.5 0.2
8 4.4 2.9 1.4 0.2
9 4.9 3.1 1.5 0.1
10 NaN NaN NaN NaN
11 4.8 3.4 1.6 0.2
12 4.8 3.0 1.4 0.1
13 4.3 3.0 1.1 0.1
14 5.8 4.0 1.2 0.2
15 5.7 4.4 1.5 0.4
16 5.4 3.9 1.3 0.4
17 5.1 3.5 1.4 0.3
18 5.7 3.8 1.7 0.3
19 5.1 3.8 1.5 0.3

1. Dropna¶

In [10]:
df.dropna(inplace = True)
In [10]:
df.isnull().sum()
Out[10]:
sepal length (cm)    0
sepal width (cm)     0
petal length (cm)    0
petal width (cm)     0
dtype: int64
In [11]:
data = load_iris()
df = pd.DataFrame(data.data, columns = data.feature_names)
df.iloc[::10] = np.nan
df.isnull().sum()
Out[11]:
sepal length (cm)    15
sepal width (cm)     15
petal length (cm)    15
petal width (cm)     15
dtype: int64

2. Fillna¶

In [12]:
df.fillna(0, inplace = True)
df.isnull().sum()
Out[12]:
sepal length (cm)    0
sepal width (cm)     0
petal length (cm)    0
petal width (cm)     0
dtype: int64
In [13]:
df.head(20)
Out[13]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 0.0 0.0 0.0 0.0
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
5 5.4 3.9 1.7 0.4
6 4.6 3.4 1.4 0.3
7 5.0 3.4 1.5 0.2
8 4.4 2.9 1.4 0.2
9 4.9 3.1 1.5 0.1
10 0.0 0.0 0.0 0.0
11 4.8 3.4 1.6 0.2
12 4.8 3.0 1.4 0.1
13 4.3 3.0 1.1 0.1
14 5.8 4.0 1.2 0.2
15 5.7 4.4 1.5 0.4
16 5.4 3.9 1.3 0.4
17 5.1 3.5 1.4 0.3
18 5.7 3.8 1.7 0.3
19 5.1 3.8 1.5 0.3

3. Fillna using bfill¶

In [14]:
data = load_iris()
df = pd.DataFrame(data.data, columns = data.feature_names)
df.iloc[::10] = np.nan
df.isnull().sum()
Out[14]:
sepal length (cm)    15
sepal width (cm)     15
petal length (cm)    15
petal width (cm)     15
dtype: int64
In [15]:
df.fillna(method = 'bfill', inplace = True)
df.isnull().sum()
/tmp/ipykernel_12621/1599715688.py:1: FutureWarning: DataFrame.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.
  df.fillna(method = 'bfill', inplace = True)
Out[15]:
sepal length (cm)    0
sepal width (cm)     0
petal length (cm)    0
petal width (cm)     0
dtype: int64
In [16]:
df.head(20)
Out[16]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 4.9 3.0 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
5 5.4 3.9 1.7 0.4
6 4.6 3.4 1.4 0.3
7 5.0 3.4 1.5 0.2
8 4.4 2.9 1.4 0.2
9 4.9 3.1 1.5 0.1
10 4.8 3.4 1.6 0.2
11 4.8 3.4 1.6 0.2
12 4.8 3.0 1.4 0.1
13 4.3 3.0 1.1 0.1
14 5.8 4.0 1.2 0.2
15 5.7 4.4 1.5 0.4
16 5.4 3.9 1.3 0.4
17 5.1 3.5 1.4 0.3
18 5.7 3.8 1.7 0.3
19 5.1 3.8 1.5 0.3

4. Fillna using pad¶

In [17]:
data = load_iris()
df = pd.DataFrame(data.data, columns = data.feature_names)
df.iloc[::10] = np.nan
df.isnull().sum()
Out[17]:
sepal length (cm)    15
sepal width (cm)     15
petal length (cm)    15
petal width (cm)     15
dtype: int64
In [18]:
df.fillna(method = 'pad', inplace = True)
df.isnull().sum()
/tmp/ipykernel_12621/2285065174.py:1: FutureWarning: DataFrame.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.
  df.fillna(method = 'pad', inplace = True)
Out[18]:
sepal length (cm)    1
sepal width (cm)     1
petal length (cm)    1
petal width (cm)     1
dtype: int64
In [20]:
df.head(20)
Out[20]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 NaN NaN NaN NaN
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
5 5.4 3.9 1.7 0.4
6 4.6 3.4 1.4 0.3
7 5.0 3.4 1.5 0.2
8 4.4 2.9 1.4 0.2
9 4.9 3.1 1.5 0.1
10 4.9 3.1 1.5 0.1
11 4.8 3.4 1.6 0.2
12 4.8 3.0 1.4 0.1
13 4.3 3.0 1.1 0.1
14 5.8 4.0 1.2 0.2
15 5.7 4.4 1.5 0.4
16 5.4 3.9 1.3 0.4
17 5.1 3.5 1.4 0.3
18 5.7 3.8 1.7 0.3
19 5.1 3.8 1.5 0.3
In [21]:
df.fillna(method = 'bfill', inplace = True)
df.isnull().sum()
/tmp/ipykernel_12621/1599715688.py:1: FutureWarning: DataFrame.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.
  df.fillna(method = 'bfill', inplace = True)
Out[21]:
sepal length (cm)    0
sepal width (cm)     0
petal length (cm)    0
petal width (cm)     0
dtype: int64
In [22]:
df.head(20)
Out[22]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 4.9 3.0 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
5 5.4 3.9 1.7 0.4
6 4.6 3.4 1.4 0.3
7 5.0 3.4 1.5 0.2
8 4.4 2.9 1.4 0.2
9 4.9 3.1 1.5 0.1
10 4.9 3.1 1.5 0.1
11 4.8 3.4 1.6 0.2
12 4.8 3.0 1.4 0.1
13 4.3 3.0 1.1 0.1
14 5.8 4.0 1.2 0.2
15 5.7 4.4 1.5 0.4
16 5.4 3.9 1.3 0.4
17 5.1 3.5 1.4 0.3
18 5.7 3.8 1.7 0.3
19 5.1 3.8 1.5 0.3

5. Imputer method¶

In [23]:
data = load_iris()
df = pd.DataFrame(data.data, columns = data.feature_names)
df.iloc[::10] = np.nan
df.isnull().sum()
Out[23]:
sepal length (cm)    15
sepal width (cm)     15
petal length (cm)    15
petal width (cm)     15
dtype: int64
In [24]:
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(strategy = 'mean')
df_imputed = imputer.fit_transform(df)
df_imputed = pd.DataFrame(df_imputed, columns = df.columns)
In [25]:
df_imputed.isnull().sum()
Out[25]:
sepal length (cm)    0
sepal width (cm)     0
petal length (cm)    0
petal width (cm)     0
dtype: int64
In [26]:
df_imputed.head(20)
Out[26]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.837778 3.054815 3.751852 1.194074
1 4.900000 3.000000 1.400000 0.200000
2 4.700000 3.200000 1.300000 0.200000
3 4.600000 3.100000 1.500000 0.200000
4 5.000000 3.600000 1.400000 0.200000
5 5.400000 3.900000 1.700000 0.400000
6 4.600000 3.400000 1.400000 0.300000
7 5.000000 3.400000 1.500000 0.200000
8 4.400000 2.900000 1.400000 0.200000
9 4.900000 3.100000 1.500000 0.100000
10 5.837778 3.054815 3.751852 1.194074
11 4.800000 3.400000 1.600000 0.200000
12 4.800000 3.000000 1.400000 0.100000
13 4.300000 3.000000 1.100000 0.100000
14 5.800000 4.000000 1.200000 0.200000
15 5.700000 4.400000 1.500000 0.400000
16 5.400000 3.900000 1.300000 0.400000
17 5.100000 3.500000 1.400000 0.300000
18 5.700000 3.800000 1.700000 0.300000
19 5.100000 3.800000 1.500000 0.300000
In [28]:
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(strategy = 'median')
df_imputed = imputer.fit_transform(df)
df_imputed = pd.DataFrame(df_imputed, columns = df.columns)
In [29]:
df_imputed.isnull().sum()
Out[29]:
sepal length (cm)    0
sepal width (cm)     0
petal length (cm)    0
petal width (cm)     0
dtype: int64
In [30]:
df_imputed.head(20)
Out[30]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.8 3.0 4.3 1.3
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
5 5.4 3.9 1.7 0.4
6 4.6 3.4 1.4 0.3
7 5.0 3.4 1.5 0.2
8 4.4 2.9 1.4 0.2
9 4.9 3.1 1.5 0.1
10 5.8 3.0 4.3 1.3
11 4.8 3.4 1.6 0.2
12 4.8 3.0 1.4 0.1
13 4.3 3.0 1.1 0.1
14 5.8 4.0 1.2 0.2
15 5.7 4.4 1.5 0.4
16 5.4 3.9 1.3 0.4
17 5.1 3.5 1.4 0.3
18 5.7 3.8 1.7 0.3
19 5.1 3.8 1.5 0.3
In [31]:
df_imputed.loc[df_imputed.index == 70]
Out[31]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
70 5.8 3.0 4.3 1.3