简体   繁体   English

使用loc,Pandas浮动值问题

[英]Pandas float value issues using loc

This might be an easy one for Pandas users used to float numbers but doing my head in and I honestly will appreciate your advise. 对于Pandas用户来说,这可能是一个简单的用户,但是我会尽力而为,我真的很感激你的建议。

I am unable to retrieve the value I saved into the dataframe when using .loc 使用.loc时,我无法检索保存到数据框中的值

Can someone please explain and help resolve? 有人可以解释并帮助解决吗? Thanks! 谢谢!

dict = [{'me': 0.094092328767113}]
df = pandas.DataFrame(dict)
df['me']
Out[32]: 
0    0.094092328767113
Name: me, dtype: float64
df.loc[0,'me']
Out[33]: 0.094092328767113001

You can use basic string formatting - 您可以使用基本字符串格式 -

>>> '{:.15f}'.format(df.loc[0,'me'])
>>> '0.094092328767113'

This would result in a string data type. 这将导致字符串数据类型。 Further, you can convert it to float if you need to using numpy as - 此外,如果你需要使用numpy ,你可以将它转换为float -

>>> np.float64('{:.15f}'.format(df.loc[0,'me']))
>>> 0.094092328767113

The final fix boils down to upgrading the version of Pandas and NumPy as confirmed by OP in the comments. 最终修复归结为升级版本的Pandas和NumPy,如OP在评论中所确认的那样。

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

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