布尔值在Pandas数据帧中建立索引

时间:2017-08-22 10:42:25

标签: python pandas dataframe indexing boolean

我得到了以下转换为数据帧的numpy数组:

data =np.array([('210', 0.5316666570181647, 0.99102639737063),
                ('215', 0.5541666565152505, 0.9906073078204338),
                ('220', 0.5658333229211469, 0.9905192216775841),
                ('225', 0.6158333218035598, 0.9893290692391012),
                ('230', 0.10499999988824131, 0.9999143781512333),
                ('235', 0.061666665288309254, 0.9999999088637485),
                ('240', 0.061666665288309254, 0.9999999088637485),
                ('245', 0.061666665288309254, 0.9999999088637485)], 
                dtype=[('index', '|O'), ('time', '<f8'), ('min_value', 
                '<f8')])

df = pd.DataFrame(data)

现在我需要获取只有min_values小于1.0的行我尝试了以下但是它没有用!

minf[minf.min_value < 1] 

1 个答案:

答案 0 :(得分:2)

看一下您的数据,很明显混淆的原因是浮动显示的方式。 min_value中的每个值都小于1,但在显示时,其中一些值是四舍五入的:

In [1131]: df
Out[1131]: 
  index      time  min_value
0   210  0.531667   0.991026
1   215  0.554167   0.990607
2   220  0.565833   0.990519
3   225  0.615833   0.989329
4   230  0.105000   0.999914
5   235  0.061667   1.000000
6   240  0.061667   1.000000
7   245  0.061667   1.000000

df.min_value < 1注册时所有人都在1以下,因为您处理的是实际值,而不是正在打印的内容。

In [1133]: df.min_value < 1
Out[1133]: 
0    True
1    True
2    True
3    True
4    True
5    True
6    True
7    True
Name: min_value, dtype: bool

作为解决方案,请考虑对数字应用舍入。然后,您可以过滤掉这些数字。例如,您可以使用np.around并舍入到小数点后5位:

In [1136]: df[np.around(df.min_value, 5) < 1]
Out[1136]: 
  index      time  min_value
0   210  0.531667   0.991026
1   215  0.554167   0.990607
2   220  0.565833   0.990519
3   225  0.615833   0.989329
4   230  0.105000   0.999914

这样,过滤器将应用于舍入数据,但不会对实际数据进行任何更改/修改