Introduction
Sometimes, datasets contain values stored in the wrong format. For example, dates may be stored as text, numbers may contain incorrect symbols, or values in the same column may use different formats. These problems can affect calculations and data analysis.
Pandas Cleaning Wrong Format provides functions such as to_datetime() and astype() to convert values into the correct data types. In this chapter, you will learn how to clean data with incorrect formats using Pandas.
What is Wrong Format in Pandas?
Wrong format means a value is stored in a format that does not match the type of information it represents.
For example:
| Name | Date | Age | Salary |
|---|---|---|---|
| Amit | 2026-01-15 | 25 | 35000 |
| Neha | 15/02/2026 | 28 | 40000 |
| Rahul | 2026-03-10 | 24 | 32000 |
In this dataset, dates use different formats. This can make date-based analysis difficult.
Other examples include:
- Numbers stored as text.
- Dates stored as strings.
- Inconsistent date formats.
- Numeric values containing unwanted characters.
Creating a DataFrame with Wrong Formats
Let’s create a DataFrame containing dates and numerical values stored in different formats.
Example:
import pandas as pd
data = {
"Name": ["Amit", "Neha", "Rahul", "Priya"],
"Date": ["2026-01-15", "15/02/2026", "2026-03-10", "20/04/2026"],
"Age": ["25", "28", "24", "27"],
"Salary": ["35000", "40000", "32000", "45000"]
}
df = pd.DataFrame(data)
print(df)
Output:
Name Date Age Salary
0 Amit 2026-01-15 25 35000
1 Neha 15/02/2026 28 40000
2 Rahul 2026-03-10 24 32000
3 Priya 20/04/2026 27 45000
Explanation: The Date, Age, and Salary columns contain text values. The dates also follow different formats. We will convert these columns into suitable data types.
Checking Data Types Using dtypes
Before cleaning a dataset, it is useful to check the data type of each column.
The dtypes attribute displays the data type of every column in a DataFrame.
Example:
print(df.dtypes)
Output:
Name object
Date object
Age object
Salary object
dtype: object
Explanation: All four columns have the object data type. This happens because the Age and Salary values are stored as text rather than numerical values.
Converting Text into Numbers Using astype()
The astype() function converts a column from one data type to another.
For example, you can convert a column containing numbers stored as text into integers.
Syntax:
df["Column_Name"] = df["Column_Name"].astype("int")
Example:
df["Age"] = df["Age"].astype("int")
print(df)
Output:
Name Date Age Salary
0 Amit 2026-01-15 25 35000
1 Neha 15/02/2026 28 40000
2 Rahul 2026-03-10 24 32000
3 Priya 20/04/2026 27 45000
Explanation: The astype("int") function converts the Age column from text into integer values.
Converting Salary into Numeric Values
You can also use astype() to convert salary values stored as text into numbers.
Example:
df["Salary"] = df["Salary"].astype("int")
print(df.dtypes)
Output:
Name object
Date object
Age int64
Salary int64
dtype: object
Explanation: The Age and Salary columns are now stored as integers. This allows you to perform numerical calculations on them.
Converting Dates Using to_datetime()
Pandas provides the to_datetime() function to convert date values into the datetime data type.
Syntax:
pd.to_datetime(data)
Example:
df["Date"] = pd.to_datetime(df["Date"], format="mixed", dayfirst=True)
print(df)
Output:
Name Date Age Salary
0 Amit 2026-01-15 25 35000
1 Neha 2026-02-15 28 40000
2 Rahul 2026-03-10 24 32000
3 Priya 2026-04-20 27 45000
Explanation:
pd.to_datetime()converts text values into dates.format="mixed"allows Pandas to interpret different date formats in the same column.dayfirst=Truetells Pandas to interpret the day before the month in ambiguous date values.
Note: The format="mixed" option is available in modern Pandas versions. If you are using an older version, you may need to convert different date formats separately.
Checking the Corrected Data Types
After converting the values, you can check whether the columns have the correct data types.
Example:
print(df.dtypes)
Output:
Name object
Date datetime64[ns]
Age int64
Salary int64
dtype: object
Explanation:
Nameremains an object because it contains text.Dateis nowdatetime64[ns]because it contains date and time values.AgeandSalaryare integers.
Converting Decimal Values Using astype()
Sometimes, numerical columns contain decimal values stored as text. You can convert them into floating-point numbers using astype("float").
Example:
data = {
"Product": ["Laptop", "Mouse", "Keyboard"],
"Price": ["55000.50", "800.75", "1500.25"]
}
df = pd.DataFrame(data)
df["Price"] = df["Price"].astype("float")
print(df)
Output:
Product Price
0 Laptop 55000.50
1 Mouse 800.75
2 Keyboard 1500.25
Explanation: The astype("float") function converts the Price column into decimal numbers, allowing you to perform calculations with decimal values.
Handling Incorrect Numeric Values Using to_numeric()
Sometimes, a numerical column contains invalid text values such as "Unknown" or "Not Available". In such cases, astype() may produce an error.
Pandas provides to_numeric() to convert values into numbers.
Example:
data = {
"Name": ["Amit", "Neha", "Rahul", "Priya"],
"Salary": ["35000", "40000", "Unknown", "45000"]
}
df = pd.DataFrame(data)
df["Salary"] = pd.to_numeric(df["Salary"], errors="coerce")
print(df)
Output:
Name Salary
0 Amit 35000.0
1 Neha 40000.0
2 Rahul NaN
3 Priya 45000.0
Explanation:
pd.to_numeric()converts values into numerical data.errors="coerce"converts invalid values intoNaN.- The
"Unknown"value becomes a missing value instead of causing an error.
You can handle these missing values later using the techniques learned in the previous chapter.
Student Task
Create a DataFrame named employees using the following data:
| Name | Joining Date | Age | Salary |
|---|---|---|---|
| Amit | 2026-01-10 | 25 | 35000 |
| Neha | 15/02/2026 | 28 | 40000 |
| Rahul | 2026-03-20 | 24 | 32000 |
| Priya | 25/04/2026 | 27 | 45000 |
| Karan | 2026-05-15 | 30 | 50000 |
Your Tasks:
- Create a Pandas DataFrame using the given employee data.
- Check the data types using
dtypes. - Convert the Age column into integers using
astype(). - Convert the Salary column into integers using
astype(). - Convert the Joining Date column into datetime values using
to_datetime(). - Display the updated DataFrame.
- Check the data types again using
dtypes.
Key Takeaways
- Wrong format means data is stored in an unsuitable or inconsistent format.
dtypeshelps identify the data type of each column.astype()converts a column into a specified data type.astype("int")converts values into integers.astype("float")converts values into decimal numbers.to_datetime()converts values into date and time formats.to_numeric()converts values into numerical data.errors="coerce"converts invalid numerical values intoNaN.
Frequently Asked Questions (FAQs)
1. What Is Pandas Cleaning Wrong Format in Python?
Pandas Cleaning Wrong Format is the process of correcting inconsistent or incorrect data formats in a DataFrame. For example, converting date values stored as text into proper date formats or changing numerical values stored as strings into numbers.
2. How to Fix Wrong Format in Pandas DataFrame?
You can fix Wrong Format in Pandas using functions such as astype() and pd.to_datetime(). These functions help convert columns into the correct data types and formats.
3. What Is the Use of Pandas astype() Function?
The Pandas astype() function is used to change the data type of a DataFrame column or an entire DataFrame. For example, you can convert a string column into an integer or float column.
4. How to Perform Cleaning Wrong Format in Pandas?
You can perform Cleaning Wrong Format in Pandas by identifying columns with incorrect data types and converting them into suitable formats using astype() or pd.to_datetime().
5. How to Convert String Values into Integers Using Pandas?
You can convert string values into integers using the astype(int) method, provided all values can be converted into valid integers.
df["Age"] = df["Age"].astype(int)
6. How to Check Data Types Before Pandas Data Cleaning?
You can check data types before Pandas Data Cleaning using the dtypes attribute or the info() method. These help identify columns containing incorrect or unexpected data types.
7. Why Is Cleaning Wrong Format Important in Pandas?
Cleaning Wrong Format in Pandas is important because incorrect data types can cause calculation errors, incorrect comparisons, and problems during data analysis. Correct formatting makes datasets easier to process and analyze.
8. What Is the Difference Between astype() and to_datetime() in Pandas?
The astype() method converts DataFrame columns into specified data types, such as integers, floats, and strings. The to_datetime() function converts date and time values into Pandas datetime format. Both are useful for Pandas Data Cleaning.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
