为什么舍入会在dict对象中消失?

时间:2017-12-05 17:15:29

标签: python

我有一只熊猫系列:

freq_features = num_occurrences/num_nodes
print(type(freq_features))
print(freq_features)

<class 'pandas.core.series.Series'>
PAT    0.386667
NUR    0.360000
MED    0.146667
ADM    0.106667

我将每个值四舍五入到小数点后两位:

freq_features_rounded = freq_features.round(2)
print type(freq_features_rounded)
print freq_features_rounded

<class 'pandas.core.series.Series'>
PAT    0.39
NUR    0.36
MED    0.15
ADM    0.11
dtype: float64

但是当我使用to_dict()命令时,我得到非舍入值(ADM键除外):

freq_features_dict = freq_features_rounded.to_dict()
print freq_features_dict
{'NUR': 0.35999999999999999, 'ADM': 0.11, 'PAT': 0.39000000000000001, 'MED': 0.14999999999999999}

为什么会发生这种情况,如何将我的舍入值作为值添加到dict()对象中?

0 个答案:

没有答案