How to format sum() output in Python dataframe

Asked

Viewed 332 times

-2

I would like to format the result of sum().

When I use the following command:

# Valor concedido de aposentadoria por ano
df.groupby('Ano')['Vlr Benefícios Concedidos (R$)'].sum()

It returns me the following result:

Ano
1996 --- 1.646539e+08
1997 --- 2.360492e+08
1998 --- 1.730143e+08
1999 --- 8.525424e+07
2000 --- 7.506213e+07

I would like to format these numbers so that they are for example 1.646.539,00.

  • What would df? dataframe? Although the answer is accepted, the question is incomplete.

1 answer

2


You can format the column using the method apply:

df.groupby('Ano')['Vlr Benefícios Concedidos (R$)'].sum().apply(lambda x: "{:,.2f}".format(x))

That shows the following format: 1,646,539.00

To get the dots as thousands separator (1.646.539,00) it is necessary to change a little for something like:

df.groupby('Ano')['Vlr Benefícios Concedidos (R$)'].sum().apply(lambda x: "{:_.2f}".format(x).replace('.', ',').replace('_', '.'))

Browser other questions tagged

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