You are currently viewing Data cleaning and preprocessing with Pandas

Data cleaning and preprocessing with Pandas

Data cleaning and preprocessing are critical steps in data analysis as they ensure the data is of high quality and ready for analysis. Here are some basic steps for data cleaning and preprocessing with Pandas:

  1. Handling missing values: You can handle missing values using various functions such as isnull(), fillna(), and dropna(). isnull() returns a Boolean mask that indicates whether each value is missing or not. fillna() fills missing values with a specified value, and dropna() removes rows with missing values.
  2. Handling duplicates: You can handle duplicate rows using the duplicated() and drop_duplicates() functions. duplicated() returns a Boolean mask that indicates whether each row is a duplicate or not, and drop_duplicates() removes duplicate rows.
  3. Handling outliers: You can handle outliers using statistical methods such as the z-score or interquartile range (IQR) method. The z-score method identifies data points that are more than a specified number of standard deviations from the mean, and the IQR method identifies data points that are outside the range of the middle 50% of the data.
  4. Normalizing and scaling data: You can normalize or scale data using various methods such as min-max scaling, z-score normalization, and log transformation. Normalization and scaling ensure that data is on a similar scale and can be compared and analyzed effectively.
  5. Encoding categorical variables: You can encode categorical variables using functions such as get_dummies() and LabelEncoder(). get_dummies() creates a binary column for each category, and LabelEncoder() assigns a numeric value to each category.
  6. Handling Datetime data: You can handle Datetime data using functions such as to_datetime() and strftime(). to_datetime() converts a string or numeric value to a DateTime object, and strftime() formats a DateTime object into a string.

By following these steps, you can effectively clean and preprocess data using Pandas and prepare it for analysis.

Here’s some sample code to demonstrate data cleaning and preprocessing with Pandas.

# Import the Pandas library
import pandas as pd

# Load data from a CSV file
df = pd.read_csv('sales_data.csv')

# Drop rows with missing values
df = df.dropna()

# Remove duplicate rows
df = df.drop_duplicates()

# Identify and remove outliers using the IQR method
Q1 = df['Revenue'].quantile(0.25)
Q3 = df['Revenue'].quantile(0.75)
IQR = Q3 - Q1
df = df[(df['Revenue'] >= Q1 - 1.5 * IQR) & (df['Revenue'] <= Q3 + 1.5 * IQR)]

# Normalize and scale data using min-max scaling
df['Revenue_normalized'] = (df['Revenue'] - df['Revenue'].min()) / (df['Revenue'].max() - df['Revenue'].min())

# Encode categorical variables using get_dummies()
df = pd.get_dummies(df, columns=['Product', 'Region'])

# Convert the 'Date' column to a datetime object and extract month and year information
df['Date'] = pd.to_datetime(df['Date'])
df['Month'] = df['Date'].dt.month
df['Year'] = df['Date'].dt.year

# Drop the original 'Date' column
df = df.drop('Date', axis=1)

In this code, we first import the Pandas library and load data from a CSV file called sales_data.csv into a Pandas data frame called df. We then drop rows with missing values using the dropna() function and remove duplicate rows using the drop_duplicates() function.

Example: Let’s consider a sample dataset containing information about customers and their orders.


To clean and preprocess this data, we can use Pandas to perform the following steps:

1. Load the data into a Pandas DataFrame.

import pandas as pd

df = pd.read_csv('orders.csv')

2. Check for missing values.


This will return the number of missing values in each column of the DataFrame. If there are any missing values, we can either fill them in using the fillna() function or drop the rows or columns containing missing values using the dropna() function.

3. Check for duplicates:


This will return the number of duplicate rows in the DataFrame. If there are any duplicate rows, we can drop them using the drop_duplicates() function.

4. Encode categorical variables:

In the sample dataset, product_name is a categorical variable. We can encode it using the get_dummies() function:

df = pd.get_dummies(df, columns=['product_name'])

This will create a new column for each unique value of product_name.

5. Handle datetime data:

In the sample dataset, order_date is a string that represents a date. We can convert it to a datetime object using the to_datetime() function:

df['order_date'] = pd.to_datetime(df['order_date'])

We can also extract information from the DateTime object, such as the day of the week or the month, using the .dt accessor:

df['day_of_week'] = df['order_date'].dt.day_name()
df['month'] = df['order_date'].dt.month

6. Normalize and scale data:

If the data contains variables on different scales, we can normalize or scale them to make them comparable. For example, we can use min-max scaling to scale the product_price column to a range of 0 to 1:

df['product_price_scaled'] = (df['product_price'] - df['product_price'].min()) / (df['product_price'].max() - df['product_price'].min())

Example of data cleaning and preprocessing using Pandas

import pandas as pd

# load the data into a Pandas DataFrame
df = pd.read_csv('sales_data.csv')

# check for missing values

# fill in missing values with the mean of the column
df['quantity'].fillna(df['quantity'].mean(), inplace=True)

# check for duplicates

# drop duplicate rows

# encode categorical variables
df = pd.get_dummies(df, columns=['product_type'])

# convert date column to datetime object
df['order_date'] = pd.to_datetime(df['order_date'])

# extract month and day of week from date column
df['month'] = df['order_date'].dt.month
df['day_of_week'] = df['order_date'].dt.day_name()

# normalize numerical data
df['quantity_norm'] = (df['quantity'] - df['quantity'].min()) / (df['quantity'].max() - df['quantity'].min())
df['price_norm'] = (df['price'] - df['price'].min()) / (df['price'].max() - df['price'].min())

This code performs the following preprocessing steps.

  1. Loads the data from a CSV file into a Pandas DataFrame.
  2. Checks for missing values and fill them in with the mean of the column.
  3. Checks for duplicate rows and drops them.
  4. Encodes categorical variables using one-hot encoding.
  5. Converts the date column to a DateTime object and extracts the month and day of the week.
  6. Normalizes the numerical data using min-max scaling.

More examples of data cleaning and preprocessing using Pandas.

Handling missing values

# load the data into a Pandas DataFrame
df = pd.read_csv('sales_data.csv')

# fill in missing values with the mode of the column
df['product_type'].fillna(df['product_type'].mode()[0], inplace=True)

# drop rows with missing values

# interpolate missing values using linear interpolation

Handling outliers

# load the data into a Pandas DataFrame
df = pd.read_csv('sales_data.csv')

# calculate the z-score for each data point
z_scores = (df['quantity'] - df['quantity'].mean()) / df['quantity'].std()

# identify and remove outliers
df = df[(z_scores >= -3) & (z_scores <= 3)]

Scaling numerical data

# load the data into a Pandas DataFrame
df = pd.read_csv('sales_data.csv')

# standardize the numerical data
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
df[['quantity', 'price']] = scaler.fit_transform(df[['quantity', 'price']])

Encoding categorical variables

# load the data into a Pandas DataFrame
df = pd.read_csv('sales_data.csv')

# encode categorical variables using label encoding
from sklearn.preprocessing import LabelEncoder

encoder = LabelEncoder()
df['product_type'] = encoder.fit_transform(df['product_type'])

# encode categorical variables using one-hot encoding
df = pd.get_dummies(df, columns=['day_of_week'])

These are just a few examples of the many data cleaning and preprocessing techniques that can be performed using Pandas. The specific techniques used will depend on the characteristics of the data and the goals of the analysis.