Python pandas formatting

0
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 | BMI | 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 the pd.round (decimal), however, I would like to use other formatting next to that which would be pd.sort_values (by = 'IMC'). I can not use both at the same time and then print already reformatted.

    
asked by anonymous 09.01.2018 / 02:52

1 answer

0

Speak Thomas Caio, all right?

I think 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 print,

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

If I'm making a mistake, say hello.

    
10.01.2018 / 21:49