How to turn all Dataset into monetary values, or at least round them

Asked

Viewed 38 times

0

This is a part of a dataset column:

0     1.347688e+05

1     1.563599e+05

2     1.788441e+05

3     1.983543e+05

I tried that:

from babel.numbers import format_decimal

format_decimal(df.Receita_liquida, locale='en_US')

but I got this>>> [<class 'decimal.ConversionSyntax'>]

If I do:

format_decimal(1.347688e+05, locale='en_US')

returns>>> 134,768.8

**that would be enough, so I need to optimize.

  • Good afternoon, John with what you are reading this dataframe?

  • Good evening I am reading with pandas, the file is in xlsx format.

1 answer

2


but I got this>>> [<class 'decimal.ConversionSyntax'>]

You are passing the entire column of the Dataframe as a parameter to the function format_decimal, who expects a number.

Use the method apply() of the Dataframe passing the function that should be applied to each element of the column and assign the result back to the column Receita_liquida.

df['Receita_liquida'] = df['Receita_liquida'].apply(format_decimal, locale = 'en_US')

Or, using a lambda function (unnecessary, but makes it easier to understand):

df['Receita_liquida'] = df['Receita_liquida'].apply(lambda x: format_decimal(x, locale='en_US'))

That’s the most you can suggest with what you put in the question.

  • 1

    I get it. It worked out! Thank you so much for helping Guilherme !!

Browser other questions tagged

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