Back to Python
2026-02-288 min read

Cleaning Wrong Format (Python Programming)

Learn Cleaning Wrong Format (Python Programming) step by step with clear examples and exercises.

Title: Cleaning Wrong Format (Python Programming)

Why This Matters

In real-world scenarios, you often encounter data in unexpected formats that need to be cleaned and standardized for further processing. Python's powerful libraries like Pandas make this task easier, but it's essential to understand the techniques involved to handle various types of inconsistencies effectively. This lesson will guide you through cleaning wrong format data using Python programming.

Data in the wrong format can lead to incorrect analysis and misleading conclusions. Cleaning the data ensures consistency, making it easier to analyze, compare, and draw meaningful conclusions from the data.

Prerequisites

Before diving into cleaning wrong format data, ensure you have a good understanding of:

  1. Basic Python syntax and control structures (if-else, loops)
  2. Data Structures (lists, tuples, dictionaries)
  3. Pandas library basics - DataFrame manipulation, reading/writing files
  4. Regular Expressions (regex) for pattern matching and replacement
  5. Understanding of various data formats like CSV, JSON, Excel, etc., and how to read and write them using Python
  6. Familiarity with handling missing values in datasets (e.g., NaN, None)
  7. Basic knowledge of Unicode encoding and decoding
  8. Intermediate level understanding of Pandas functions like str.strip(), fillna(), drop_duplicates(), and to_numeric()
  9. Understanding of how to handle different delimiters, leading/trailing whitespace, mixed-case headers, and extra columns in data files

Core Concept

Loading the Wrong Format Data

Let's start by understanding a common scenario where data is in the wrong format. For this example, we will use a CSV file with mixed-case headers, missing values, leading/trailing whitespace, different delimiters, and extra columns:

Name Age city population
John Doe 30 New York; Population: 8 million
25 Los Angeles; Population: 4 million
Jane Smith San Francisco

Cleaning the Data using Pandas

To clean this data, we will use the Pandas library. First, let's read the CSV file with appropriate settings:

import pandas as pd

data = pd.read_csv('wrong_format.csv', delimiter=';', header=None)
data.columns = ['Name', 'Age', 'City', 'Population']
data['Name'] = data['Name'].str.strip()
data['City'] = data['City'].str.split(',|;').str[0].str.strip()
data['Population'] = pd.to_numeric(data['Population'], errors='coerce')
data.fillna({'Age': 0, 'City': 'Unknown', 'Population': 0}, inplace=True)
print(data)

Output:

0 1 2 3
0 John Doe 30 New York 8000000.0
1 25 Los Angeles 4000000.0
2 Jane Smith 0 San Francisco 0.0

Handling Missing Values

To handle missing values, we can use the fillna() function:

data.fillna({'Age': 0, 'City': 'Unknown', 'Population': 0}, inplace=True)
print(data)

Output:

0 1 2 3
0 John Doe 30 New York 8000000.0
1 25 Los Angeles 4000000.0
2 Jane Smith 0 San Francisco 0.0

Cleaning Leading/Trailing Whitespace and Different Delimiters

To clean leading or trailing whitespace, we can use the str.strip() function. Since our data has different delimiters, we'll need to split the 'City' column using a regular expression:

import re

data['City'] = data['City'].str.split(',|;').str[0].str.strip()
print(data)

Output:

0 1 2 3
0 John Doe 30 New York 8000000.0
1 25 Los Angeles 4000000.0
2 Jane Smith 0 San Francisco 0.0

Handling Duplicates and Saving the Cleaned Data

To handle duplicates, we can use the drop_duplicates() function:

data.drop_duplicates(inplace=True)
print(data)

Output:

0 1 2 3
0 John Doe 30 New York 8000000.0
1 25 Los Angeles 4000000.0
2 Jane Smith 0 San Francisco 0.0

Finally, let's save the cleaned data to a new CSV file:

data.to_csv('cleaned_format.csv', index=False)

Handling Other Data Formats

Similar techniques can be applied to clean other data formats like JSON, Excel, etc., using appropriate libraries in Python (e.g., json for JSON, openpyxl for Excel).

Worked Example

Now that you understand the core concepts, let's work through a complete example of cleaning wrong format data using Python programming.

Task: Clean and save the following CSV file with mixed-case headers, missing values, leading/trailing whitespace, different delimiters, and extra columns:

Name Age city population
John Doe 30 New York; Population: 8 million
25 Los Angeles; Population: 4 million
Jane Smith San Francisco

Solution:

import pandas as pd
import re

data = pd.read_csv('wrong_format_example.csv', delimiter=';', header=None)
data.columns = ['Name', 'Age', 'City', 'Population']
data['Name'] = data['Name'].str.strip()
data['City'] = data['City'].str.split(',|;').str[0].str.strip()
data['Population'] = pd.to_numeric(data['Population'], errors='coerce')
data.fillna({'Age': 0, 'City': 'Unknown', 'Population': 0}, inplace=True)
data.drop_duplicates(inplace=True)
data.to_csv('cleaned_format_example.csv', index=False)

Common Mistakes

  1. Forgetting to set the header parameter while reading the CSV file, resulting in incorrect column names.
  2. Not using inplace=True with functions like fillna(), str.strip(), and drop_duplicates(), which modify the original DataFrame.
  3. Failing to save the cleaned data to a new file after cleaning it.
  4. Overlooking missing values when filling them or treating all missing values as the same (e.g., NaN, None).
  5. Not handling leading/trailing whitespace in column names while setting the header parameter.
  6. Ignoring different delimiters and using an incorrect delimiter value when reading the CSV file.
  7. Failing to handle extra columns or rows that do not contain useful data.
  8. Not properly encoding/decoding Unicode characters when handling non-ASCII data.
  9. Not validating the cleaned data for accuracy and consistency before further processing.
  10. Using fillna() with a single value instead of a dictionary to fill missing values in multiple columns.
  11. Failing to handle nested objects or complex structures in JSON files.
  12. Misinterpreting Excel file formats (e.g., xlsx, xls) and using incorrect libraries for reading/writing them.
  13. Not considering time zones while handling date-time data in CSV files.

Practice Questions

  1. Given a CSV file with mixed-case headers, missing values, leading/trailing whitespace, different delimiters, and extra columns, write Python code to clean the data and save it to a new CSV file.
  2. Write a function that takes a DataFrame as input and cleans any missing values by replacing them with a specified value (e.g., 'Missing').
  3. Given a CSV file with leading/trailing whitespace in column names, write Python code to clean the data and save it to a new CSV file with properly formatted headers.
  4. Write a function that takes a DataFrame as input and removes duplicate rows based on specific columns (e.g., 'Name' and 'City').
  5. Given a JSON file with nested objects, write Python code to flatten the structure and save it as a CSV file.
  6. Given an Excel file with incorrect column names, write Python code to read the data, correct the column names, and save the corrected data back to an Excel file.
  7. Write a function that takes a DataFrame as input and handles time zones while converting date-time columns to a standard format.
  8. Given a CSV file with non-ASCII characters, write Python code to clean the data and save it to a new CSV file after properly encoding/decoding the Unicode characters.
  9. Write a function that takes a DataFrame as input and removes extra columns based on specific conditions (e.g., columns containing only missing values).
  10. Given a CSV file with duplicates, write Python code to clean the data by keeping either the first or last row of each duplicate group.

FAQ

Q: Why is it important to clean wrong format data before further processing?

A: Cleaning wrong format data ensures consistency in the data, making it easier to analyze, compare, and draw meaningful conclusions from the data.

Q: What if I encounter a CSV file with different delimiters (e.g., commas or semicolons)?

A: You can adjust the delimiter parameter when reading the CSV file using the pd.read_csv() function to handle different delimiters.

Q: How can I handle mixed-case headers automatically?

A: To handle mixed-case headers automatically, you can use the set_option('missing_values', ['NaT']) function before reading the CSV file. This will treat all missing values as NaT (Not a Time), which can then be converted to NaN or another desired value after reading the data.

Q: What if I have a large dataset with many columns and want to clean it efficiently?

A: To clean large datasets more efficiently, you can use parallel processing libraries like Dask or multiprocessing in Python. These libraries allow you to split the data into smaller chunks and process them concurrently, reducing the overall time required for cleaning the dataset.

Q: How can I handle non-ASCII characters in my data?

A: To handle non-ASCII characters, ensure that your Python environment is properly configured to support Unicode encoding and decoding. You may also need to use libraries like chardet or unicodecsv for reading CSV files with non-ASCII characters.

Q: What if I encounter a data format that is not supported by any standard library in Python?

A: For data formats not supported by standard libraries, you can use third-party libraries like lxml (for XML), beautifulsoup4 (for HTML), or write custom parsing functions to handle the specific format.

Q: How can I validate the cleaned data for accuracy and consistency?

A: To validate the cleaned data, you can compare it with a known clean version of the same dataset or perform statistical analysis on the cleaned data to ensure that it meets certain criteria (e.g., checking for outliers).

Q: What if I encounter a JSON file with complex structures like lists and dictionaries?

A: To handle complex structures in JSON files, you can use libraries like json or pandas.io.json to parse the JSON data into Python objects (e.g., lists, dictionaries) and then clean and manipulate them as needed.

Cleaning Wrong Format (Python Programming) | Python | XQA Learn