A comprehensive data cleaning library for pandas DataFrames
Project description
data_cleaner_lib: A Comprehensive Python Data Cleaning Library
data_cleaner_lib is a robust and intuitive Python library designed to streamline the often complex and time-consuming process of data cleaning for pandas DataFrames. It provides a comprehensive suite of tools to identify and rectify common data quality issues, including missing values, duplicates, outliers, and inconsistent text formats, with careful consideration for various edge cases. This library aims to empower data professionals and analysts to prepare their datasets for analysis and machine learning models with greater efficiency and reliability.
Features
data_cleaner_lib offers a versatile set of functionalities to tackle diverse data cleaning challenges:
- Handle Missing Values: Impute or remove missing data using a variety of strategies:
'mean': Replace missing numerical values with the column's mean.'median': Replace missing numerical values with the column's median.'mode': Replace missing values (numerical or categorical) with the most frequent value.'drop': Remove rows containing missing values in specified columns.'custom'(value): Fill with a user-specified constant value.
- Remove Duplicates: Identify and eliminate redundant rows based on all columns or a specified subset, ensuring data uniqueness with options to keep
'first','last', or no duplicates. - Handle Outliers: Detect and mitigate the impact of extreme values using statistical methods:
'iqr'(Interquartile Range): Cap values outside 1.5×IQR from quartiles.'zscore': Replace outliers with the median based on a Z-score threshold (default 0.8), with additional clipping forage(≥100) andsalary(≥1e6).
- Standardize Text Data: Clean and normalize string columns to ensure consistency:
- Convert to lowercase.
- Strip leading/trailing whitespace.
- Remove extra internal whitespace.
- Handle special characters and non-alphanumeric data.
- Optionally preserve email formats (retaining
@and.) during standardization.
- Convert Data Types: Safely cast columns to desired data types (e.g., int, float, datetime), with error handling and automatic NaN filling for integer conversions.
- Preprocess Mixed-Type Columns: Automatically detect and handle columns with mixed data types, converting empty strings to NaN and attempting numeric or datetime conversions where appropriate.
- Generate Cleaning Reports: Obtain a comprehensive summary of cleaning operations, including original and current DataFrame shapes, missing values, and data types.
- Chaining Operations: The API supports method chaining for fluid, sequential application of cleaning steps.
Installation
You can install data_cleaner_lib using pip:
pip install data_cleaner_lib
Requirements
- Python >= 3.8
- pandas >= 1.5.0
- numpy >= 1.21.0
- scipy >= 1.7.0
Usage
Here's a quick example demonstrating how to use data_cleaner_lib to clean a DataFrame:
from data_cleaner_lib.cleaner import DataCleaner
import pandas as pd
# 1. Load or create a DataFrame
# For demonstration, we'll create a sample dataset with various data quality issues.
df = pd.DataFrame({
'id': [1, 2, 2, 3, 4],
'name': ['John Doe', 'Jane Smith', 'Jane Smith', 'Bob@123', None],
'email': ['john@doe.com', ' jane.smith@email.com ', 'jane.smith@email.com', 'bob@123.com', 'invalid@'],
'age': [25, '30', None, 150, -5],
'salary': [50000, 60000, 60000, 1e9, None],
'category': ['A', 'B', 'B', None, 'C'],
'date_joined': ['2023-01-01', '2023-02-01', '2023-02-01', 'invalid', None]
})
print("Original DataFrame Head:")
print(df.head())
print("\nOriginal DataFrame Info:")
df.info()
# 2. Initialize the DataCleaner with your DataFrame
cleaner = DataCleaner(df)
# 3. Apply cleaning operations sequentially
# Preprocess columns that might have mixed data types
print("\nApplying preprocess_mixed_types...")
cleaner.preprocess_mixed_types()
# Standardize text columns (e.g., 'name', 'email', 'category')
# preserve_email=True ensures email addresses are not corrupted during standardization.
print("\nApplying standardize_text...")
cleaner.standardize_text(columns=['name', 'email', 'category'], preserve_email=True)
# Remove duplicate rows based on a subset of columns
print("\nApplying remove_duplicates...")
cleaner.remove_duplicates(subset=['id', 'name'])
# Handle missing values: fill numerical NaNs with the median, and categorical NaNs with the mode
print("\nApplying handle_missing_values...")
cleaner.handle_missing_values(strategy='median', columns=['age', 'salary'])
cleaner.handle_missing_values(strategy='mode', columns=['category'])
# Handle outliers in numerical columns using the Z-score method
# Values with a Z-score > 0.8 or < -0.8 will be replaced with the median.
print("\nApplying handle_outliers...")
cleaner.handle_outliers(columns=['age', 'salary'], method='zscore')
# Convert data types for specific columns
print("\nApplying convert_dtypes...")
cleaner.convert_dtypes({
'age': 'int32',
'salary': 'float64',
'date_joined': 'datetime64[ns]'
})
# 4. View results and get the cleaned DataFrame
print("\n--- Cleaning Report ---")
print(cleaner.cleaning_report())
print("\n--- Cleaned DataFrame Head ---")
cleaned_df = cleaner.get_cleaned_data()
print(cleaned_df.head())
print("\n--- Cleaned DataFrame Info ---")
cleaned_df.info()
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Note: The example DataFrame above is for demonstration purposes. Replace it with your actual data loading mechanism (e.g.,
pd.read_csv('your_data.csv')).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file data_cleaner_lib-1.1.tar.gz.
File metadata
- Download URL: data_cleaner_lib-1.1.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8426cb4b89a095e59309d5b5a772d0ffe58496331f54dd7674207e3e45ca7866
|
|
| MD5 |
ad65ed2a2bda78f42563d4a007fcfc20
|
|
| BLAKE2b-256 |
3aa2ba938099e5134c9c4ed55d3d9c32f7ac9bba505b99bb97b70a780702cdc5
|
File details
Details for the file data_cleaner_lib-1.1-py3-none-any.whl.
File metadata
- Download URL: data_cleaner_lib-1.1-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
756fc691859cd662fc3cc304cf9ea1a9e8035d4fbb12e186d08b0e48a8345db2
|
|
| MD5 |
ee8fc78258344fb20a146ae5d5e77a52
|
|
| BLAKE2b-256 |
729061f69936e5c9a9fdddf0c58feace71105d9c6fd471f72af791dfc95965cb
|