Pre-processing

Data Cleaning and Pre-processing with pandas

Introduction

Data cleaning and preprocessing are essential steps in any data analysis workflow. Raw data is often messy, incomplete, or inconsistent, and preprocessing ensures it’s in the right format for analysis. In this post, we will explore common techniques for handling missing data, removing duplicates, and transforming data using pandas.


1. Handling Missing Data

Missing data is one of the most common issues in real-world datasets. pandas provides several methods to identify and deal with missing values.

Detecting Missing Data

You can detect missing data using the .isnull() method, which returns a DataFrame of the same shape with True for missing values and False otherwise.

python
Copy code
import pandas as pd

# Example DataFrame with missing values
data = {
    'Name': ['Alice', 'Bob', 'Charlie', None],
    'Age': [25, None, 35, 40],
    'City': ['New York', 'Los Angeles', None, 'Chicago']
}
df = pd.DataFrame(data)

# Detect missing values
print(df.isnull())

Filling Missing Data

To handle missing data, you can either fill in the gaps with a default value (e.g., mean, median) or use forward/backward filling.

  • Filling with a constant value:
python
Copy code
df['Age'] = df['Age'].fillna(30)  # Fill missing values with 30
  • Filling with the mean:
python
Copy code
df['Age'] = df['Age'].fillna(df['Age'].mean())  # Fill with the mean age
  • Forward filling: Propagate the last valid observation forward:
python
Copy code
df = df.fillna(method='ffill')
  • Backward filling: Propagate the next valid observation backward:
python
Copy code
df = df.fillna(method='bfill')

Dropping Missing Data

If missing data is too prevalent, you may choose to drop rows or columns with missing values:

  • Dropping rows with missing values:
python
Copy code
df = df.dropna()  # Drop rows with any missing values
  • Dropping columns with missing values:
python
Copy code
df = df.dropna(axis=1)  # Drop columns with any missing values

2. Removing Duplicates

Duplicate entries can often occur during data collection, and they can distort analysis results. The .duplicated() method helps you identify duplicates.

Identifying Duplicates

python
Copy code
# Identify duplicate rows
print(df.duplicated())

Removing Duplicates

You can remove duplicate rows using the .drop_duplicates() method. By default, it removes all duplicate rows:

python
Copy code
df = df.drop_duplicates()

If you want to remove duplicates based on specific columns, you can pass the column names as an argument:

python
Copy code
df = df.drop_duplicates(subset=['Name', 'Age'])

3. Converting Data Types

Data types play a crucial role in how data is processed. Sometimes, data might be read as the wrong type (e.g., numbers as strings). You can convert data types using pandas.

Converting a Column to a Different Data Type

python
Copy code
# Convert 'Age' to integer
df['Age'] = df['Age'].astype(int)

Handling Categorical Data

If your data contains categorical variables, converting them to the category type can save memory and improve performance:

python
Copy code
df['City'] = df['City'].astype('category')

4. String Manipulation

Often, your data might have string columns that need cleaning, such as trimming extra spaces, changing case, or replacing substrings.

Removing Leading/Trailing Whitespaces

python
Copy code
df['City'] = df['City'].str.strip()  # Remove leading/trailing spaces

Converting to Lowercase

python
Copy code
df['City'] = df['City'].str.lower()  # Convert to lowercase

Replacing Substrings

python
Copy code
df['City'] = df['City'].str.replace('los', 'LA')  # Replace 'los' with 'LA'

Extracting Substrings

You can extract specific parts of a string using regular expressions:

python
Copy code
df['Initial'] = df['City'].str.extract(r'(\b\w)')  # Extract first letter

5. Handling Date and Time Data

Date and time are essential in many datasets, and pandas provides powerful tools for working with them.

Converting to Datetime

If a date column is stored as a string, you can convert it to datetime type:

python
Copy code
df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')

Extracting Date Components

Once you have a datetime column, you can extract specific parts of the date:

python
Copy code
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
df['Day'] = df['Date'].dt.day

6. Handling Outliers

Outliers are values that significantly deviate from the rest of the data and can skew analysis. Handling outliers depends on the context of the data and analysis goals.

Identifying Outliers with Z-scores

The Z-score represents how many standard deviations a data point is from the mean. You can calculate Z-scores and filter out extreme values.

python
Copy code
from scipy import stats

z_scores = stats.zscore(df['Age'])
df['Z-Score'] = z_scores
outliers = df[df['Z-Score'].abs() > 3]  # Identifying values more than 3 standard deviations from the mean
print(outliers)

7. Example: Cleaning and Preprocessing Data

Let’s apply some of these techniques to a real-world example. Suppose you have a dataset of employees with missing data, duplicates, and inconsistencies in city names.

python
Copy code
import pandas as pd

# Example DataFrame
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Alice'],
    'Age': [25, 30, None, 40, 25],
    'City': ['New York ', 'Los Angeles', 'NEW YORK', 'Los angeles ', 'New York'],
    'Date': ['2021-07-01', '2021-06-15', '2021-05-20', None, '2021-07-01']
}
df = pd.DataFrame(data)

# Step 1: Clean string columns
df['City'] = df['City'].str.strip().str.lower()

# Step 2: Handle missing values in 'Age' and 'Date'
df['Age'] = df['Age'].fillna(df['Age'].mean())  # Fill missing 'Age' with the mean
df['Date'] = pd.to_datetime(df['Date'], errors='coerce')  # Convert 'Date' to datetime, handle invalid formats

# Step 3: Remove duplicate rows
df = df.drop_duplicates()

# Resulting clean data
print(df)

Conclusion

Data cleaning and preprocessing are essential skills for any data analyst or scientist. By using pandas, you can handle missing data, remove duplicates, transform data types, and prepare your data for analysis. These techniques ensure that your dataset is clean, consistent, and ready for insightful analysis.