Using python/pandas to search dataframe rows containing both a user-specified integer and approximated float value

zachn

I am using pandas to read a text file filled with numerical data. I need to search two columns for user specified values (a specified constant integer value and an approximate float value) to return the rows which uniquely identify each dataset. I am currently able to return the rows containing a specified column integer value using:

import pandas as pd

integer = some integer
df = pd.read_csv("...")

array = df[(df[i] == integer)]

This successfully returns all rows containing a particular integer value and assigns to a dataframe named "array".

I am unable, however, to return rows which contain a specified floating point value using the same method. It simply returns an empty array, although I know that a row containing my test value is present in the data.

Additionally, I don't simply want to search for an exact float value, but I need to search for an approximate float value in the columns. For example, say the nominal user-specified value is '.6', the experimental value may actually have been '.59993' or '.60004'. So I need for the user to input '.6' and python search for values which are slightly greater than or less than .6.

Here is an example of what I have tried:

import pandas as pd

some_float = .6
df = pd.read_csv("...")

array = df[(df[i] <= some_float+.01 and >= some_float-.01)]

All attempts at using the various operators have resulted in a ValueError: "the truth value of an series is ambiguous. Use a.empty, a.any() or a.all()." However, this may be partly due to it not reading float values at all. Ultimately, the dataset which is "extracted" from the initial dataframe will be uniquely identified by a specified integer value and a float value.

Thanks

DSM

I tend to check whether the absolute value of the difference is less than some tolerance:

>>> df = pd.DataFrame({"A": np.random.random(1000), "B": np.arange(1000) % 4})
>>> some_float = 0.6
>>> abs_tol = 0.001
>>> df[(df.A - some_float).abs() < abs_tol]
            A  B
66   0.600845  2
180  0.600577  0
922  0.599571  2

Or if you want both the float and int comparison:

>>> df[((df.A - some_float).abs() < abs_tol) & (df.B == 0)]
            A  B
180  0.600577  0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

If Condition for Both Integer and float

From Dev

Partition by approximated value

From Java

Add identical rows to a Spark Dataframe using an integer

From Dev

Remove several rows from dataframe using search

From Java

R - Deleting both rows in a "paired" dataframe if one value = x

From Dev

C# convert string to float containing both "," and "."

From Dev

Check if float value is integer

From Dev

Loading text file containing both float and string using numpy.loadtxt

From Dev

Drop rows if value in a specific column is not an integer in pandas dataframe

From Dev

'find' files containing an integer in a specified range (in bash)

From Java

ANLTR4: parse both integer and float

From Dev

How to divide float by integer when both are variables?

From Dev

How to divide float by integer when both are variables?

From Dev

Represent float using integer

From Dev

Extracting rows containing specific value using mapReduce and hadoop

From Dev

python dataframe converts integer to float

From Dev

How to search a partial "value" in a map containing String values and Integer keys in Java?

From Dev

MySQL full text search, joining and ordering both by relevancy and an integer, using PHP and PDO

From Dev

MySQL full text search, joining and ordering both by relevancy and an integer, using PHP and PDO

From Dev

Rounding up a dataframe consist of string and float both

From Dev

Delete rows not containing a value in pandas

From Dev

Delete rows not containing a value in pandas

From Dev

Give a number to return the approximated value of an Enum?

From Dev

Replace integer(0) in the cells of a dataframe containing lists

From Dev

Regex to find strings not containing a specified value

From Dev

How do I create a function that will accept a pandas dataframe and remove rows containing a specific value?

From Dev

Using Java Fx, what is the best way to search for an integer value when creating a search filter?

From Dev

Writing both float and integer on a text file from one numpy array

From Dev

Mapping float value to function depending on specified intervals

Related Related

  1. 1

    If Condition for Both Integer and float

  2. 2

    Partition by approximated value

  3. 3

    Add identical rows to a Spark Dataframe using an integer

  4. 4

    Remove several rows from dataframe using search

  5. 5

    R - Deleting both rows in a "paired" dataframe if one value = x

  6. 6

    C# convert string to float containing both "," and "."

  7. 7

    Check if float value is integer

  8. 8

    Loading text file containing both float and string using numpy.loadtxt

  9. 9

    Drop rows if value in a specific column is not an integer in pandas dataframe

  10. 10

    'find' files containing an integer in a specified range (in bash)

  11. 11

    ANLTR4: parse both integer and float

  12. 12

    How to divide float by integer when both are variables?

  13. 13

    How to divide float by integer when both are variables?

  14. 14

    Represent float using integer

  15. 15

    Extracting rows containing specific value using mapReduce and hadoop

  16. 16

    python dataframe converts integer to float

  17. 17

    How to search a partial "value" in a map containing String values and Integer keys in Java?

  18. 18

    MySQL full text search, joining and ordering both by relevancy and an integer, using PHP and PDO

  19. 19

    MySQL full text search, joining and ordering both by relevancy and an integer, using PHP and PDO

  20. 20

    Rounding up a dataframe consist of string and float both

  21. 21

    Delete rows not containing a value in pandas

  22. 22

    Delete rows not containing a value in pandas

  23. 23

    Give a number to return the approximated value of an Enum?

  24. 24

    Replace integer(0) in the cells of a dataframe containing lists

  25. 25

    Regex to find strings not containing a specified value

  26. 26

    How do I create a function that will accept a pandas dataframe and remove rows containing a specific value?

  27. 27

    Using Java Fx, what is the best way to search for an integer value when creating a search filter?

  28. 28

    Writing both float and integer on a text file from one numpy array

  29. 29

    Mapping float value to function depending on specified intervals

HotTag

Archive