python - 十进制 'dot' 到 'comma',使用 2 位小数
python - decimal 'dot' to 'comma', use 2 decimals
我有这个数据,“VALOR”是浮点数:
Periodo
VALOR
32021
1096.14
32021
3835.44
32021
2207.90
32021
389.10
我正在尝试将小数 'dot' 更改为 'comma',没问题。
但是我需要保存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
这里怎么取两位小数?
也试过这个:
pd.options.display.float_format = '{:,.2f}'.format
还有这个
df['VALOR'] = df['VALOR'].apply(lambda x: locale.format('%.2f', x))
None 他们成功了。我需要将小数转换为逗号并保留 2 位小数。
也许一些enconding会解决?比如将我的系统更改为 UTF-8?已经尝试过..但还没有。
此致,
田井
尝试使用 apply()
方法、replace()
方法和 rstrip()
方法:
df['VALOR']=df['VALOR'].apply(lambda x: '{:.2f},'.format(x))
df['VALOR'] = df['VALOR'].str.replace('.',',',regex=True)
df['VALOR']=df['VALOR'].str.rstrip(',')
现在如果你打印 df
你会得到:
Periodo VALOR
0 32021 1096,14
1 32021 3835,44
2 32021 2207,90
3 32021 389,10
我有这个数据,“VALOR”是浮点数:
Periodo | VALOR |
---|---|
32021 | 1096.14 |
32021 | 3835.44 |
32021 | 2207.90 |
32021 | 389.10 |
我正在尝试将小数 'dot' 更改为 'comma',没问题。
但是我需要保存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 |
这里怎么取两位小数?
也试过这个:
pd.options.display.float_format = '{:,.2f}'.format
还有这个
df['VALOR'] = df['VALOR'].apply(lambda x: locale.format('%.2f', x))
None 他们成功了。我需要将小数转换为逗号并保留 2 位小数。
也许一些enconding会解决?比如将我的系统更改为 UTF-8?已经尝试过..但还没有。
此致,
田井
尝试使用 apply()
方法、replace()
方法和 rstrip()
方法:
df['VALOR']=df['VALOR'].apply(lambda x: '{:.2f},'.format(x))
df['VALOR'] = df['VALOR'].str.replace('.',',',regex=True)
df['VALOR']=df['VALOR'].str.rstrip(',')
现在如果你打印 df
你会得到:
Periodo VALOR
0 32021 1096,14
1 32021 3835,44
2 32021 2207,90
3 32021 389,10