Francisco Ghelfi
Francisco Ghelfi

Reputation: 962

Formatting Pandas decimals in Django

I´m not able to format some numbers with fixed decimal positions on Pandas dataframe in Django.

I´ve tryied several options:

round(testdataframe["ticket_medio"], 1)

Result: unsupported operand type(s) for *: 'decimal.Decimal' and 'float'

np.round(testdataframe["ticket_medio"], decimals=1)

Result: unsupported operand type(s) for *: 'decimal.Decimal' and 'float'

testdataframe.round(1)

Result: nothing seems to happen

pd.options.display.float_format = '{:.1f}'.format
testdataframe.round(1)

Result: nothing seems to happen

Upvotes: 1

Views: 272

Answers (1)

Francisco Ghelfi
Francisco Ghelfi

Reputation: 962

Like @Borut explained, the solution was first to change my Pndas dataframe values to numeric:

testdataframe['ticket_medio'] = pd.to_numeric(testdataframe['ticket_medio'])

Then, in the template you should use {{ your_value|floatformat:"0" }} to actualy showing the number with your desired decimal points.

Upvotes: 1

Related Questions