Python pandas formatting

Asked

Viewed 851 times

1

import numpy as np
import pandas as pd
ID = [i for i in range(1,101)]
def tabela(imc):
    if imc < 18.5:
        return 'Abaixo do peso'
    elif imc < 25 and imc >= 18.5:
        return 'Saudável'
    elif imc < 30 and imc >= 25:
        return 'Acima do peso'
    elif imc < 35 and imc >= 30:
        return 'Obesidade grau I'
    elif imc < 40 and imc >= 35:
        return 'Obesidade grau II'
    else:
        return 'Obesidade grau III'

pd.options.display.max_rows = 999
altura, peso=np.loadtxt('data.csv',delimiter=':',dtype='float',unpack=True)
imc = peso/altura**2
classificacao = []
for i in imc:
    classificacao.append(tabela(i))
data = {'Altura' : altura,
        'Peso' : peso,
        'IMC' : imc,
        'Classificação': classificacao,
        'Chamada':chamada}

df = pd.DataFrame(data,columns = ['Altura','Peso','IMC','Classificação'],index=data['ID'])
decimal = pd.Series([2,2,1],index=['Altura','Peso','IMC'])
df.sort_values(by='IMC')
print(df.round(decimal))

table/output:

  • Height | Weight | IMC | Classification
  • 1 1.88 | 91.45 | 25.8 | Overweight
  • 2 1.82 | 95.65 | 29.0 | Overweight
  • 3 1.88 | 94.01 | 26.5 | Overweight
  • 4 1.74 | 63.55 | 21.0 | Healthy
  • 5 1.69 | 66.12 | 23.1 | Healthy
  • 6 1.75 | 68.84 | 22.5 | Healthy
  • 7 1.75 | 82.72 | 26.9 | Overweight

.....

The values are only formatted this way due to pd.round(decimal), however, I would like to use another formatting next to the one that would be pd.sort_values(by='IMC'). I can’t use both at the same time and then print already reformatted.

1 answer

1


It’s Thomas Caio, all right?

I believe you could include the line "df.sort_values(by='IMC')" to a variable, and then apply the "round" on top of this variable next to the print, getting:

novodf = df.sort_values(by='IMC')
print(novodf.round(decimal))

In case I’m mistaken, say hello, hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.