Deleting Entire blank row in an existing Excel Sheet using Python

Jd16 picture Jd16 · Jan 4, 2016 · Viewed 8.2k times · Source

How to remove the entire blank row from the existing Excel Sheet using Python?

I need a solution which DOES NOT : Include reading the whole file and rewriting it without the deleted row.

IS THERE ANY DIRECT SOLUTION?

Answer

Jd16 picture Jd16 · Jan 5, 2016

I achieved using Pandas package....

import pandas as pd

#Read from Excel
xl= pd.ExcelFile("test.xls")

#Parsing Excel Sheet to DataFrame
dfs = xl.parse(xl.sheet_names[0])

#Update DataFrame as per requirement
#(Here Removing the row from DataFrame having blank value in "Name" column)

dfs = dfs[dfs['Name'] != '']

#Updating the excel sheet with the updated DataFrame

dfs.to_excel("test.xls",sheet_name='Sheet1',index=False)