-
Notifications
You must be signed in to change notification settings - Fork 1
/
Handling missing values and outlier values in a dataset in Python.txt
73 lines (47 loc) · 2.86 KB
/
Handling missing values and outlier values in a dataset in Python.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Importing Libraries
To begin with handling missing and outlier values, you will need to import the necessary libraries. The most commonly used library for this purpose is pandas. You can import pandas using the following code:
import pandas as pd
Loading Data
Before you can start handling missing and outlier values, you will need to load the data into a pandas DataFrame. You can do this using the pandas read_csv function, or by loading data from another source such as a database.
# Load data from a CSV file
df = pd.read_csv('data.csv')
Handling Missing Values
You can use the isnull method to identify cells with missing values in a DataFrame. This method returns a boolean mask indicating which cells contain missing values.
# Identify cells with missing values
df.isnull()
# Count the number of missing values in each column
df.isnull().sum()
Once you have identified the missing values, you can handle them in several ways. One option is to simply remove the rows containing missing values using the dropna method.
# Drop rows with any missing values
df.dropna()
# Drop rows with all missing values
df.dropna(how='all')
# Drop rows with at least n non-missing values
df.dropna(thresh=n)
Another option is to fill the missing values with a scalar value using the fillna method.
# Fill missing values with a scalar value
df.fillna(value)
# Fill missing values with a different value for each column
df.fillna({'col_1': value_1, 'col_2': value_2})
# Forward-fill missing values
df.ffill()
# Backward-fill missing values
df.bfill()
Handling Outlier Values
There are several ways to identify outlier values in a dataset. One common method is to use the interquartile range (IQR), which is defined as the difference between the 75th percentile and the 25th percentile of the data. Values that are outside of the range defined by the IQR are considered to be outliers.
You can use the describe method too
# Handling Outlier Values
# Compute the IQR
iqr = df['column'].describe()['75%'] - df['column'].describe()['25%']
# Remove outlier values
df.drop(df[(df['column'] < df['column'].describe()['25%'] - 1.5*iqr) |
(df['column'] > df['column'].describe()['75%'] + 1.5*iqr)].index)
# Replace outlier values with the median
df['column'].mask((df['column'] < df['column'].describe()['25%'] - 1.5*iqr) |
(df['column'] > df['column'].describe()['75%'] + 1.5*iqr),
df['column'].median())
# Replace outlier values with the minimum or maximum non-outlier value
df['column'].mask((df['column'] < df['column'].describe()['25%'] - 1.5*iqr) |
(df['column'] > df['column'].describe()['75%'] + 1.5*iqr),
df['column'].clip(lower=df['column'].describe()['25%'] - 1.5*iqr,
upper=df['column'].describe()['75%'] + 1.5*iqr))