• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

107
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)
about 3 years 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.

about 3 years 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()
about 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda