how to count number of filled cells in each column of excel using python

Tags: , , ,



I have a sheet in Excel which is not structured data as you can see here .

I tried using xlrd package

total_rows=worksheet.nrows
total_cols=worksheet.ncols

but this is giving me the maximum row count for a column. But I need filled cell count for each column. Could anyone suggest solution.

Answer

You can try to iterate through every row and check whether the cell is empty or not. You can use this:

import xlrd
book = xlrd.open_workbook('excel.xls')
sheet = book.sheet_by_index(0)

count = 0
for row in range(sheet.nrows):
    for col in sheet.row_values(row):
        if col.strip() != '':
            count += 1


Source: stackoverflow