Remove Empty Rows from Excel file in Python using pandas

Hello friends, we have got several empty rows in datasets. To remove all of them is a tedious process. You can do that easily using the pandas library. In this tutorial, I will tell you how you can remove empty rows in Excel using Python.

Remove Empty Rows in Excel using Pandas

In my other tutorial, we learned to Find Empty Cells in Excel using Python. Let’s remove the empty rows, which often result in erroneous statistics. I’ve taken the Cust_details.xlsx Excel file for example.

Remove Empty Rows from Excel file in Python using pandas

If you have not installed Pandas yet you can install that using the below pip command:

pip install pandas

To remove the empty rows from the Excel file, I’ve used the dropna() function. You need to add the function as an attribute for your Python object which contains your Excel file data. The parameter axis determines whether the column’s or row’s null values are to be removed. axis = 0 is for deleting rows while axis = 1 is for deleting columns with null values. To perform row’s deletion I’ve set the axis values to 0. The parameter how determines if the row or columns should be removed if at least one value is empty or if all of them are empty.

I want to remove the row only if both Cust_ID and Cust_Name‘s value is empty, so I have set the how value to 'all'.

Code :

df = file.dropna(axis = 0, how = 'all')

print(df)

Output :

   Cust_ID         Cust_Name
0   ID2008     Reyansh Mitra
1   ID9801    Richard Larson
3   ID6510               NaN
5   ID4509  Payal Chatterjee
8      NaN      Has Anderson
11  ID5409      Kejriwal Lal

After executing the code, I got the data with no empty rows. You can now write this data onto an Excel file using the to_excel() function.

Leave a Reply

Your email address will not be published. Required fields are marked *