0

I have an excel spreadsheet whose data are as follows (from A1 to C3): original spreadsheet I want to calculate the chances of none-zero values in a column, then write the result to the last cell in this column. In the case of the third column, the result should be 2/3 = 0.67 Below is the Python script that I wrote to do the same thing, but it gets the wrong result obviously.

The code:

import openpyxl
wb = openpyxl.load_workbook('testXls.xlsx')
sheet = wb.active
for colNum in range(1, sheet.max_column + 1):
    coverCount = 0
    for rowNum in range(1, sheet.max_row + 1):
        if sheet.cell(row=rowNum, column=colNum).value != 0:
            coverCount += 1
    sheet.cell(row=4, column=colNum).value = round(coverCount / 3, 2)
wb.save('testXls2.xlsx')

The result: result spreadsheet

I can't find anything wrong in the code. Could someone enlighten me on this please? I really appreciate it.

1 Answer 1

1

The problem is that you write down the value in the fourth line of each iteration, which leads to unnecessary iteration.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.