SoFunction
Updated on 2025-03-04

Python Pandas can easily implement data cleaning

In today's data-driven era, data cleaning is a crucial step in data analysis and machine learning projects. Dirty data, missing values, duplicate records and other problems may seriously affect the accuracy of the results. With its powerful data processing capabilities, the Pandas library in Python has become the preferred tool for data cleaning. This article will use easy-to-understand language, concise logic and rich cases to help you easily clean up data using Python and Pandas.

1. Pandas basics and data import

Pandas is an open source data analysis and operation library for Python, providing high-performance, easy-to-use data structures and data analysis tools. It is built on NumPy and is suitable for processing table data such as CSV, Excel files, etc.

1. Install Pandas

First, make sure you have the Pandas library installed. If it has not been installed, you can use the following command to install it:

pip install pandas

2. Import Pandas

import pandas as pd

3. Data import

Pandas provides a variety of methods to import data, such as from CSV, Excel, SQL database, etc. Here is an example of importing data from a CSV file:

df = pd.read_csv('')

2. Data preview and preliminary analysis

Before doing data cleaning, it is crucial to understand the structure and content of the data. Pandas provides a variety of ways to help us quickly preview our data.

1. View the first few lines of data

print(())

2. View the data column name

print()

3. View data shape

print()

4. View basic data statistics

print(())

Through these methods, we can have a preliminary understanding of the structure, type, missing values, etc. of the data, laying the foundation for subsequent data cleaning work.

3. Handle missing values

Missing values ​​are a common problem in data cleaning. Pandas provides a variety of methods to deal with missing values, such as deleting rows or columns containing missing values, filling missing values, etc.

1. Delete missing values

Use the dropna method to delete rows or columns containing missing values.

# Delete rows with missing valuesdf_drop_rows = ()
 
# Delete columns with missing valuesdf_drop_cols = (axis=1)

2. Fill in missing values

Use the fillna method to fill in missing values ​​with specified values.

# Fill in missing values ​​with 0df_fill_0 = (0)
 
# Fill in missing values ​​with the average of the columndf_fill_mean = (())

3. Interpolation padding

For time series data, interpolate filling can be performed using the interpolate method.

# Interpolate to fill missing valuesdf_interpolate = ()

4. Process duplicate values

Repeated values ​​are also issues that need to be paid attention to in data cleaning. Pandas provides duplicated methods and drop_duplicates methods to identify and process duplicate values.

1. Identify duplicate values

# Tag duplicate valuesduplicated_df = df[()]

2. Delete duplicate values

# Delete duplicate values ​​and keep the first occurrence of recordsdf_drop_duplicates = df.drop_duplicates()

3. Delete all duplicate values

# Delete all duplicate values ​​and keep only unique recordsdf_unique = df.drop_duplicates(keep=False)

5. Handle outliers

Outliers (outliers) may have a significant impact on data analysis results. Although Pandas does not have a function that directly deals with outliers, we can combine statistical methods and conditional filtering to identify and process outliers.

1. Use statistical methods to identify outliers

Typically, we can use the 3σ principle (three times standard deviation) or interquartile spacing (IQR) to identify outliers.

# Calculate the interquartile spacing (IQR)Q1 = df['column_name'].quantile(0.25)
Q3 = df['column_name'].quantile(0.75)
IQR = Q3 - Q1
 
# Identify outliersoutliers = df[(df['column_name'] < (Q1 - 1.5 * IQR)) | (df['column_name'] > (Q3 + 1.5 * IQR))]

2. Handle outliers

There are many ways to deal with outliers, such as deleting outliers, replacing outliers with average values, medians, etc.

# Delete outliersdf_no_outliers = df[~((df['column_name'] < (Q1 - 1.5 * IQR)) | (df['column_name'] > (Q3 + 1.5 * IQR)))]
 
# Replace outliers with medianmedian_value = df['column_name'].median()
df['column_name'] = df['column_name'].apply(lambda x: median_value if ((x < (Q1 - 1.5 * IQR)) | (x > (Q3 + 1.5 * IQR))) else x)

6. Data type conversion

During data cleaning, we often need to convert data types to types suitable for analysis. Pandas provides an astype method for data type conversion.

1. Convert to string type

df['column_name'] = df['column_name'].astype(str)

2. Convert to integer type

df['column_name'] = df['column_name'].astype(int)

3. Convert to floating point number type

df['column_name'] = df['column_name'].astype(float)

4. Convert to date and time type

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

7. Data standardization and normalization

In some data analysis scenarios, we need to standardize or normalize the data to eliminate the impact of different dimensions on the data analysis results.

1. Standardization

Standardization is to convert data into a distribution with a mean of 0 and a standard deviation of 1. It can be used for standardization.

from  import StandardScaler
 
scaler = StandardScaler()
df[['column1', 'column2']] = scaler.fit_transform(df[['column1', 'column2']])

2. Normalization

Normalization is to scale the data between the specified minimum and maximum values ​​(usually 0 and 1). Normalization can be used.

from  import MinMaxScaler
 
scaler = MinMaxScaler()
df[['column1', 'column2']] = scaler.fit_transform(df[['column1', 'column2']])

8. Case practice: Clean up sales data

Below, we will use an actual sales data cleaning case to comprehensively apply the above knowledge.

1. Data import and preview

df = pd.read_csv('sales_data.csv')
print(())

2. Handle missing values

# Delete rows with missing valuesdf = ()
 
# Fill in missing values ​​with specified values ​​for certain columns (such as filling in missing discount rates with 0)df['discount_rate'] = df['discount_rate'].fillna(0)

3. Process duplicate values

df = df.drop_duplicates()

4. Handle outliers

# Calculate the interquartile spacing of salesQ1 = df['sales_amount'].quantile(0.25)
Q3 = df['sales_amount'].quantile(0.75)
IQR = Q3 - Q1
 
# Delete sales outliersdf = df[~((df['sales_amount'] < (Q1 - 1.5 * IQR)) | (df['sales_amount'] > (Q3 + 1.5 * IQR)))]

5. Data type conversion

# Convert date column to datetime typedf['order_date'] = pd.to_datetime(df['order_date'])
 
# Convert discount rate to floating point typedf['discount_rate'] = df['discount_rate'].astype(float)

6. Data standardization

from  import StandardScaler
 
scaler = StandardScaler()
df[['sales_amount', 'quantity']] = scaler.fit_transform(df[['sales_amount', 'quantity']])

7. Cleaned data preview

print(())

Through the above steps, we successfully cleaned up missing values, duplicate values, and outliers in sales data, and typed and standardized the data. The cleaned data is more neat and standardized, laying a solid foundation for subsequent data analysis work.

Conclusion

Data cleaning is an indispensable step in data analysis and machine learning projects. With its powerful data processing capabilities, Pandas has become the preferred tool for data cleaning. This article allows you to easily get started with Python and Pandas through easy-to-understand language, concise logic and rich cases.

This is the end of this article about Python Pandas' easy data cleaning. For more information about Python Pandas data cleaning, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!