Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

49
Vistas
Average Excel Columns

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)
8 months ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Python and the Openpyxl API makes this kind of thing very easy.

rows = ws.iter_rows(min_row=2, max_row=6, max_col=1, values_only=True)
values = [row[0] for row in rows]
avg = sum(values) / len(values)

But you should probably check that the cells contain numbers, otherwise you'll see an exception.

8 months ago · Santiago Trujillo Denunciar

0

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']
    cellsum = 0
    for i, cell in enumerate(range1, 1): 
        print(i)
        cellsum += cell[0].value
        
    print(cellsum / i)
main()
8 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos