简体   繁体   English

python 浮点到逗号,带 2 位小数

[英]python float dot to comma with 2 decimals

Im having a trouble here.我在这里遇到了麻烦。

I have this data, "VALOR" is float:我有这个数据,“VALOR”是浮动的:

Periodo周期 VALOR勇气
32021 32021 1096.14 1096.14
32021 32021 3835.44 3835.44
32021 32021 2207.90 2207.90
32021 32021 389.10 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.但是我需要保存2个小数,当我转换它时,当最后一个小数为0时它就会消失。

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


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

How to get the 2 decimals here?如何在这里获得2位小数?

Regards,问候,

Tanai谷内

Thanks to Anurag https://stackoverflow.com/users/14289892/anurag-dabas感谢 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(',')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM