0

Im having a trouble here.

I have this data, "VALOR" is float:

Periodo VALOR
32021 1096.14
32021 3835.44
32021 2207.90
32021 389.10

I'm trying to change dot to comma, and that ok.

But I need to save the 2 decimals, and when I convert it, it desappear when the last decimal is 0.

df['VALOR'] = np.round(df['VALOR'], decimals=2).astype(str)
df['VALOR'] = df['VALOR'].str.replace('.',',')


df.head()
Periodo VALOR
32021 1096,14
32021 3835,44
32021 2207,9
32021 389,1

How to get the 2 decimals here?

Regards,

Tanai

  • 3
    `pd.options.display.float_format = '{:,.2f}'.format`, see https://stackoverflow.com/questions/42735541/customized-float-formatting-in-a-pandas-dataframe – Tim Roberts Apr 19 '21 at 23:13

1 Answers1

0

Thanks to Anurag https://stackoverflow.com/users/14289892/anurag-dabas

This works

df['VALOR']=df['VALOR'].apply(lambda x: '{:.2f},'.format(x))
df['VALOR'] = df['VALOR'].str.replace('.',',',regex=True)
df['VALOR']=df['VALOR'].str.rstrip(',')