
Data Cleaning With Python — Data Science
6 min read
·
Feb 20, 2023
--
In data science, one of the major process in arriving in a well processed data for analysis is ‘Data Cleaning. And this is where python shines when it comes to data analysis.
Data cleaning is the process of identifying and correcting or removing errors, inconsistencies, and inaccuracies in data in order to improve its quality and ensure that it is accurate and reliable for analysis. In Python, there are various libraries and tools that can be used for data cleaning. In this discourse, we will cover some of the most common steps involved in data cleaning using Python. Which are:
Importing the necessary libraries: The first step is to import the necessary libraries, including Pandas, Numpy etc. These libraries are essential for data manipulation, analysis, and visualization.
Loading the data: The next step is to load the data into a Pandas DataFrame. Pandas is a powerful library for data manipulation and analysis, and it provides several methods for loading data, including CSV, Excel, SQL, and more.
Handling missing data: Missing data is a common problem in real-world datasets, and it can be handled in various ways, including imputation, deletion, or interpolation. Pandas provides several methods for handling missing data, including fillna, dropna, and interpolate.
Removing duplicates: Duplicates can distort data analysis and lead to incorrect conclusions. Pandas provides a method called drop_duplicates that can be used to remove duplicate rows from a DataFrame.
In this project, we will be using Pandas to clean our data so that it is ready for analysis. Three different types of data issues that we will deal with are: Missing Data, Duplicate Data, Incorrect Data.
Importing and Correcting data type
To take care of these data issues, we are able to use built in pandas functions to clean it efficiently. Before starting to clean the above, we can use the
info() function to give us a snapshot of the type of data that is in our DataFrame and systematically change data into proper data types.We can first start by seeing that the
Date column is an object type, whereas the correct type for a date is datetime. We can correct this by inputting the follow code.We can now see that the date has been corrected to right data type.
Missing Data
Using the
info() function above, we can see there are null values in our data from looking at the Non-Null Count column. Some columns have a different non-null count, which means that these columns have more null values. To see in greater detail which columns and rows have null values, we can use the isna() function.This output can be difficult to interpret and act on. A way to simplify this information is to pair `isna()` with the `sum()` function.
Now, we can see the number of null values per each column.
Missing data can be sorted out using the following approach:
Removing null values from DataFrame rows and/or columns
Filling null values with a constant or other values from the DataFrame
Removing Null Values
To remove columns or rows that contain null values, we can use the
dropna() function. We can start by dropping the rows where at least one element is null in the row. This reduces our DataFrame from 1887 rows to 1684 rowss as seen below.From the above the number of rows in the data frame has been reduced to 1684. We can also drop columns that have null values rather than rows with the same function, but specifying the axis.
Looking above, we now see that the columns that have any null values have been dropped also.
Filling null values
Completely dropping columns & rows that have null values is usually not an efficient method of cleaning data, as lots of data can be lost.
A more common approach to cleaning these null values is to fill them in using the
fillna() function. Two popular uses of the function is:Fill the null values with 0 or any value
Fill the null values based on the next/previous number (‘ffill’ and ‘bfill’).
Forward filling and backward filling are two approaches to fill missing values. Forward filling means fill missing values with previous data. Backward filling means fill missing values with next data point.
With these methods, we can still retain valuable data and not drop the entire column/row.
I believe with this you have been able to learn how to clean your data for analysis. In the next episode I will be writing on how to handle ‘error data’, ‘duplicate data’ and ‘exporting of data’.
Data source: My sincere appreciation goes to the CFI team. The dataset has been uploaded to my google drive here.
follow me for updates on data science and analysis processes.
0 likes · 8 views · Jun 16, 2025
Data cleaning using Python libraries like Pandas and Numpy.
Timeline
Feb 11, 2023 - Feb 20, 2023