Pandas dataframe.value_counts()。keys()与浮点索引

时间:2014-03-14 16:14:58

标签: python pandas

我正在尝试使用pandas来获取value_counts。发出命令时:

my_variable.value_counts().keys()

我得到以下输出:

  

指数([1.0,0.0,1.00999999046,2.0,2.00999999046,3.0,   1.01000022888,3.00999999046,4.00999999046,4.0,6.00999999046,5.00999999046,8.00999999046,2.01000022888,5.0 0.990000009537,9.00999999046,6.0,7.0,12.0099999905,7.00999999046,10.0099999905,3.01000022888,19.0199999809,11.0099999905,20.0199999809,8.0,14.0199999809,4.01000022888,5.01000022888,38.0399999619, 46.0499999523,40.0399999619,20.0299999714,16.0199999809,18.0299999714,9.0119999998093,11.0199999809,21.0199999809,-10651.4099998,-4643.13999987,-6388.92000008,-5779.98000002],dtype = object)

问题是,如何访问由浮点值组成的键,例如键1.00999999046?

我可以使用:

访问索引1.0
my_variable.value_counts()[1]

但是,如果我尝试

my_variable.value_counts()[1.00999999046]

然后我收到错误声明:

  

KeyError:1.00999999046

我认为这可能与指数的dytpe =对象有关,但我不知道该怎么做才能解释这一点。任何指导都将不胜感激。

1 个答案:

答案 0 :(得分:2)

这在> = 0.13中工作得很好。在0.13浮点数之前,指数并不特别。他们现在有逻辑来避免将索引器舍入/截断为整数。在其他工作中,值被查找,而不是被强制(对于Float64Index)。事实上,这是这种类型索引的重点,使[],ix,loc的统一索引模型返回相同的结果。

请参阅the docs

In [8]: i = Index([1.0, 0.0, 1.00999999046, 2.0, 2.00999999046, 3.0, 1.01000022888, 3.00999999046, 4.00999999046, 4.0, 6.00999999046, 5.00999999046, 8.00999999046, 2.01000022888, 5.0, 0.990000009537, 9.00999999046, 6.0, 7.0, 12.0099999905, 7.00999999046, 10.0099999905, 3.01000022888, 19.0199999809, 11.0099999905, 20.0199999809, 8.0, 14.0199999809, 4.01000022888, 5.01000022888, 38.0399999619, 46.0499999523, 40.0399999619, 20.0299999714, 16.0199999809, 18.0299999714, 9.01999998093, 11.0199999809, 21.0199999809, -10651.4099998, -4643.13999987, -6388.92000008, -5779.98000002])

In [9]: i
Out[9]: Float64Index([1.0, 0.0, 1.00999999046, 2.0, 2.00999999046, 3.0, 1.01000022888, 3.00999999046, 4.00999999046, 4.0, 6.00999999046, 5.00999999046, 8.00999999046, 2.01000022888, 5.0, 0.990000009537, 9.00999999046, 6.0, 7.0, 12.0099999905, 7.00999999046, 10.0099999905, 3.01000022888, 19.0199999809, 11.0099999905, 20.0199999809, 8.0, 14.0199999809, 4.01000022888, 5.01000022888, 38.0399999619, 46.0499999523, 40.0399999619, 20.0299999714, 16.0199999809, 18.0299999714, 9.01999998093, 11.0199999809, 21.0199999809, -10651.4099998, -4643.13999987, -6388.92000008, -5779.98000002], dtype='object')

In [10]: s = Series(i.tolist() * 3)


In [13]: s.value_counts()[1.00999999046]
Out[13]: 3

请注意,索引的显示是值的截断视图(它们完全存在,只是不打印超出2个位置)

In [14]: s.value_counts().sort_index()
Out[14]: 
-10651.41    3
-6388.92     3
-5779.98     3
-4643.14     3
 0.00        3
 0.99        3
 1.00        3
 1.01        3
 1.01        3
 2.00        3
 2.01        3
 2.01        3
 3.00        3
 3.01        3
 3.01        3
 4.00        3
 4.01        3
 4.01        3
 5.00        3
 5.01        3
 5.01        3
 6.00        3
 6.01        3
 7.00        3
 7.01        3
 8.00        3
 8.01        3
 9.01        3
 9.02        3
 10.01       3
 11.01       3
 11.02       3
 12.01       3
 14.02       3
 16.02       3
 18.03       3
 19.02       3
 20.02       3
 20.03       3
 21.02       3
 38.04       3
 40.04       3
 46.05       3
dtype: int64

In [15]: s.value_counts()[1.00999999046]
Out[15]: 3

In [16]: s.value_counts().keys()
Out[16]: Float64Index([3.00999999046, 14.0199999809, 2.00999999046, -10651.4099998, 2.01000022888, 18.0299999714, 20.0299999714, 16.0199999809, 6.00999999046, 3.01000022888, 8.0, 11.0199999809, 19.0199999809, 7.0, 1.01000022888, 0.990000009537, 4.0, 3.0, 2.0, 1.0, 46.0499999523, 11.0099999905, 12.0099999905, 4.00999999046, 40.0399999619, 7.00999999046, 9.01999998093, 6.0, -6388.92000008, 21.0199999809, 38.0399999619, 5.0, 20.0199999809, 4.01000022888, -5779.98000002, 1.00999999046, 9.00999999046, -4643.13999987, 5.01000022888, 10.0099999905, 8.00999999046, 5.00999999046, 0.0], dtype='object')