【问题标题】:python float dot to comma with 2 decimalspython float dot to comma with 2 decimals
【发布时间】:2021-07-14 03:30:07
【问题描述】:

我在这里遇到了麻烦。

我有这个数据,“VALOR”是浮点数:

Periodo VALOR
32021 1096.14
32021 3835.44
32021 2207.90
32021 389.10

我正在尝试将点更改为逗号,这没关系。

但我需要保存2个小数,当我转换它时,它会在最后一个小数为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

如何在这里获得两位小数?

问候,

谷内

【问题讨论】:

标签: python pandas numpy etl


【解决方案1】:

感谢阿努拉格 https://stackoverflow.com/users/14289892/anurag-dabas

这行得通

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-27
    • 2016-03-18
    • 2022-12-02
    • 2022-12-26
    • 2014-01-28
    • 2019-12-18
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多