【发布时间】:2020-09-21 15:55:20
【问题描述】:
我正在尝试将 style.format 函数应用于数据框以调整小数位数,因为 round() 函数不适用。 (我不明白为什么。)
df 中的数据格式为 123.0、432.0 和 543.0,但我需要它们有两位小数(例如,123.00)。
我已使用函数df.dtypes 验证该列的类型为float。
我尝试应用以下内容:
import pandas.io.formats.style
import pandas as pd
df['DD'] = df[['Quantity']].style.format("{:.2%}")
df
但DD字段中出现以下内容:
<pandas.io.formats.style.Styler object at 0x7f...
round() 函数也不适用于列。
可以做什么?
【问题讨论】:
-
嗨@Gonza - 我删除了我之前的答案,因为自从我第一次回答以来,你已经大大改变了这个问题。您能告诉我们您的 python 设置是什么(版本号、发行版、juypyterlab/ipython 控制台/python 控制台等)吗?另外,你能解释一下为什么要对数据进行四舍五入吗?您是否只是想让数据在 python 笔记本/提示中正确显示?或者您是否尝试将数据写入文件并希望在其中反映 2 位小数精度?如果是这样,您可以发布该代码吗?
标签: python pandas format pandas-styles