简体   繁体   English

使用 iloc 在 pandas dataframe 中选择值时保持浮点精度

[英]Maintain float precision when selecting values in a pandas dataframe using iloc

My code:我的代码:

import pandas as pd 

pd.set_option('display.float_format','{:.2f}'.format)

df = pd.Series([500.0, 200.0, 300.0])
df
0   500.00
1   200.00
2   300.00
dtype: float64

df.iloc[1]
200.0

I want df.iloc[1] to return 200.00, can anyone explain what's happening here and how to get the desired result please.我希望df.iloc[1]返回 200.00,任何人都可以解释这里发生了什么以及如何获得所需的结果。

Indexing the way you did will return the actual value at that index, which is type float.以您所做的方式索引将返回该索引处的实际值,即浮点类型。

type(df.iloc[1]) # returns 'numpy.float64'

What you would like to do is print the entire row, like so:您想要做的是打印整行,如下所示:

df.iloc[[1]]

In this case, the index returns a series:在这种情况下,索引返回一个系列:

type(df.iloc[[1]]) # returns 'pandas.core.series.Series'

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

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