迭代pandas数据帧时的值不同

时间:2017-04-20 07:20:48

标签: python pandas dataframe

我有一个pandas数据帧:

     name        lat         lon
0    9999  25.147289  101.362893
1   10000  48.646000    8.701000
2   10001  48.646000    8.991000
3   10002  48.646000    9.453000
4   10003  48.646000    9.881000

当我遍历pandas数据帧并尝试获取每行的值时:

for row in df.iterrows():
    print(row[1]['lat'])

我得到25.147289000000001

为什么需要额外的数字以及如何获得25.147289

1 个答案:

答案 0 :(得分:2)

数据框中的浮点值以dtypes指定的精度存储。您看到的内容由pandas display.float_format控制,它设置为6位精度。这样做 会改变幕后的实际浮动。

如果您希望打印声明为您舍入

for row in df.iterrows():
    print('{:0.6f}'.format(row[1]['lat']))

25.147289
48.646000
48.646000
48.646000
48.646000