Handling Missing Data with Pandas

Introduction

Handling missing data is a crucial part of data analysis and cleaning. Inconsistent and absent data can lead to inaccurate analysis and predictions. Python offers robust libraries like pandas to identify, manage, and fill missing data in an efficient manner. In this lesson, we'll explore fundamental techniques of handling missing data using pandas.

Identifying Missing Data

Let's recall from the previous unit's lesson that before treating missing data, it is important to identify it. The pandas library provides several functions to detect null or missing values.

Python
import pandas as pd

# Sample DataFrame creation with missing values
data = {
    'Name': ['Alice', None, 'Charlie'],
    'Age': [25, None, 30],
    'Salary': [50000, 60000, None]
}
df = pd.DataFrame(data)

# Identifying missing data
missing_data = df.isnull()
print(missing_data)

Output:

text
    Name    Age  Salary
0  False  False   False
1   True   True   False
2  False  False    True

In the above example, df.isnull() generates a DataFrame of the same shape as df, filled with True for missing values and False for non-missing values, enabling easy identification.

Dropping Missing Data

One straightforward method to handle missing values is to drop any rows or columns containing them. This method is useful when the missing data is minimal and does not significantly affect the dataset.

Python
# Displaying the original DataFrame
print("Original DataFrame:")
print(df)

# Dropping rows with missing values
df_cleaned = df.dropna()

# Displaying the DataFrame after dropping missing value rows
print("\nDataFrame after dropping rows with missing values:")
print(df_cleaned)

The above code outputs the following:

text
Original DataFrame:
      Name   Age   Salary
0    Alice  25.0  50000.0
1     None   NaN  60000.0
2  Charlie  30.0      NaN

DataFrame after dropping rows with missing values:
    Name   Age   Salary
0  Alice  25.0  50000.0

The dropna() function eliminates any row where at least one element is missing, thus cleaning up the DataFrame for further analysis or operations. By showing the DataFrame before and after dropping missing value rows, you can clearly see the impact of this operation.

Filling Missing Data

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal