这个DataFrame名为data
...
winner
runner_name
Steve 0
Joe 0
Marsha 1
Kent 0
如果我创建一个占位符为0 ...
的新列 data['weighted'] = 0
当我传递十进制值以在循环中更新它(经过一系列长时间的计算和其他事情之后)......
data.ix[runner_name]['weighted'] = 45.88
...它会自动舍入数字。因此,框架变得......
winner weighted
runner_name
Steve 0 46
Joe 0 80
Marsha 1 100
Kent 0 80
如何防止它四舍五入?
答案 0 :(得分:2)
分配一个浮动将导致Pandas为列提供float
dtype:
data['weighted'] = 0.0
import pandas as pd
data = pd.DataFrame({'winner':[0,0,1,0]}, index=pd.Series(['Steve', 'Joe', 'Marsha', 'Kent'], name='runner_name'), )
data['weighted'] = 0.0
data.ix['Steve', 'weighted'] = 45.88
print(data)
产量
winner weighted
runner_name
Steve 0 45.88
Joe 0 0.00
Marsha 1 0.00
Kent 0 0.00
>>> print(data.dtypes)
winner int64
weighted float64
dtype: object