Home > OS >  Average Excel Columns
Average Excel Columns

Time:06-24

I am trying to average out values within a column at a certain range. I tried listing out the range as a tuple then for looping to be able to get the cell value. I then created a variable for the average but get the error 'TypeError: 'float' object is not iterable.

range1 = ws["A2":"A6]
for cell in range1:
    for x in cell:
        average = sum(x.value)/len(x.value)
        print(average)

CodePudding user response:

Something like this will get you the mean of the cells.

import openpyxl as op

def main():
    wb = op.load_workbook(filename='C:\\Users\\####\\Desktop\\SO nonsense\\Book1.xlsm')
    
    range1 = wb['Sheet1']['A2:A6']
    i = 0
    cellsum = 0
    for cell in range1: 
        cellsum  = cell[0].value 
        i  = 1
        
    print(cellsum / i)
main()
  • Related