简体   繁体   English

pandas dataframe:通过浮点范围的变量选择行

[英]pandas dataframe: Selecting rows by variable of floating point range

I am new ty Python and wanted to find a solution to select rows by a variable which should be a range of floating points.我是新手 Python 并想通过一个变量找到 select 行的解决方案,该变量应该是一个浮点范围。 As a variable I assign numpy array made with numpy arange since python's built-in range does not take float as step.作为一个变量,我分配了用 numpy arange 制作的 numpy 数组,因为 python 的内置范围不以 float 作为步骤。 Here's an example of what I tried:这是我尝试过的一个例子:

import numpy as np
import pandas as pd
data = [[1, 100], [2,200], [3, 300], [4, 400], [5, 500], 
    [5.5, 550], [6, 600], [6.5, 620]] 
df = pd.DataFrame(data, columns = ['small_number', 'big number'])

rng = np.arange(2, 7, 0.1)
df.loc[df.small_number.isin(rng)]

Unfortunately, the gives only the first value and does not find other.不幸的是, 只给出了第一个值,没有找到其他值。 The output: output:

small_number    big number
1   2.0         200

How could I make a float range variable which then will be passed to select DataFrame rows?如何创建一个浮点范围变量,然后将其传递给 select DataFrame 行?

You're going to need to round in order to check for existence in the array.您将需要四舍五入以检查数组中是否存在。 The float numbers in the array are not precise as stated in the documentation .文档中所述,数组中的浮点数并不精确。

When using a non-integer step, such as 0.1, the results will often not be consistent.

In this case we can use np.around and round to one decimal place.在这种情况下,我们可以使用np.around并四舍五入到小数点后一位。

df.loc[df.small_number.isin(np.around(rng, decimals=1))] 

   small_number  big number
1           2.0         200
2           3.0         300
3           4.0         400
4           5.0         500
5           5.5         550
6           6.0         600
7           6.5         620

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

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