简体   繁体   English

read_excel时熊猫数据框更改浮点值

[英]Pandas data frame change float values when read_excel

Hi I have a situation in hand , 嗨,我有一个情况,

I have an excel whose one column contains the following : 我有一个Excel,其一栏包含以下内容:

    DOUBLE_FI_EXC_RETURN
      0.21
     -0.52
     -0.28
     -0.01
      0.45
      0.24
     -1.12
     -0.14

Now when i do df.read_excel() it returns this data frame: 现在,当我执行df.read_excel()时,它将返回此数据帧:

DOUBLE_FI_EXC_RETURN
 0.214307422
-0.517325534
-0.28279697
-0.005493749
 0.452745819
 0.238851397
-1.116148896
-0.141628643

I think it is giving me an accurate value but i need the same values from excel . 我认为这给了我准确的价值,但我需要excel的相同价值。 How should i get them 我应该如何得到他们

You need to round the values after 2 precisions if you really want to. 如果确实需要,则需要将值四舍五入为2个精度。 Excel might be showing you values that are rounded but pandas might be capturing the exact values with correct precision. Excel可能会向您显示四舍五入的值,但pandas可能正在以正确的精度捕获确切的值。 If you really want to round the values you can do so by: 如果您确实想舍入这些值,可以通过以下方法进行:

import pandas as pd
df=pd.DataFrame({'DOUBLE_FI_EXC_RETURN':[0.214307422,
                                         -0.517325534,
                                         -0.28279697,
                                         -0.005493749,
                                          0.452745819,
                                          0.238851397,
                                         -1.116148896,
                                         -0.141628643,]})
df.DOUBLE_FI_EXC_RETURN.apply(lambda x:round(x,2))

Or using numpy 或使用numpy

np.round(df.DOUBLE_FI_EXC_RETURN, 2)

如果要降低精度,可以使用round

round(DOUBLE_FI_EXC_RETURN, 2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM