我有一个电子表格,其中包含(除其他外)行的人员及其教育水平,我已将其读入数据帧。
我正在尝试返回一个包含相对频率的字典,该频率已四舍五入到小数点后 3 位。
return self.data['education'].value_counts(normalize=True).round(3)
返回
BA/BS 0.458
Some Col 0.250
PostGrad 0.167
High Sch 0.125
Name: education, dtype: float64
但是
return self.data['education'].value_counts(normalize=True).round(3).to_dict()
返回
{u'High Sch': 0.125, u'BA/BS': 0.45800000000000002, u'PostGrad': 0.16700000000000001, u'Some Col': 0.25}
我对此感到有点困惑。谁能解释一下到底发生了什么?
最佳答案
试试这个:
pd.options.display.float_format = '{:,.3f}'.format
关于Python Pandas 输出四舍五入的 DataFrame 到字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36095185/