How to select a range of values in a pandas dataframe column?

124,151

Solution 1

Use between with inclusive=False for strict inequalities:

df['two'].between(-0.5, 0.5, inclusive=False)

The inclusive parameter determines if the endpoints are included or not (True: <=, False: <). This applies to both signs. If you want mixed inequalities, you'll need to code them explicitly:

(df['two'] >= -0.5) & (df['two'] < 0.5)

Solution 2

.between is a good solution, but if you want finer control use this:

(0.5 <= df['two']) & (df['two'] < 0.5)

The operator & is different from and. The other operators are | for or, ~ for not. See this discussion for more info.

Your statement was the same as this:

(0.5 <= df['two']) and (df['two'] < 0.5)

Hence it raised the error.

Share:
124,151
ShanZhengYang
Author by

ShanZhengYang

Updated on October 13, 2020

Comments

  • ShanZhengYang
    ShanZhengYang over 3 years
    import pandas as pd
    import numpy as np
    data = 'filename.csv'
    df = pd.DataFrame(data)
    df 
    
            one       two     three  four   five
    a  0.469112 -0.282863 -1.509059  bar   True
    b  0.932424  1.224234  7.823421  bar  False
    c -1.135632  1.212112 -0.173215  bar  False
    d  0.232424  2.342112  0.982342  unbar True
    e  0.119209 -1.044236 -0.861849  bar   True
    f -2.104569 -0.494929  1.071804  bar  False
    

    I would like to select a range for a certain column, let's say column two. I would like to select all values between -0.5 and +0.5. How does one do this?

    I expected to use

    -0.5 < df["two"] < 0.5
    

    But this (naturally) gives a ValueError:

    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
    

    I tried

    -0.5 (< df["two"] < 0.5)
    

    But this outputs all True.

    The correct output should be

    0    True
    1    False
    2    False
    3    False
    4    False
    5    True
    

    What is the correct way to find a range of values in a pandas dataframe column?

    EDIT: Question

    Using .between() with

    df['two'].between(-0.5, 0.5, inclusive=False)
    

    would would be the difference between

     -0.5 < df['two'] < 0.5
    

    and inequalities like

     -0.5 =< df['two'] < 0.5
    

    ?

    • MaxU - stop genocide of UA
      MaxU - stop genocide of UA almost 8 years
      There is a better alternative: df.query('-0.5 <= two < 0.5')
    • ShanZhengYang
      ShanZhengYang almost 8 years
      @MaxU Thanks for this! I hadn't thought of this. This is very clean
  • ShanZhengYang
    ShanZhengYang almost 8 years
    What do you mean by using inclusive=False for strict inequalities? I'm not sure I understand the difference between inclusive=True and inclusive=False?
  • ShanZhengYang
    ShanZhengYang almost 8 years
    Using between(-0.5, 0.5) , what would be the difference between -0.5 < value < 0.5 and -0.5 = < value < 0.5 ?
  • ShanZhengYang
    ShanZhengYang almost 8 years
    Thanks for the explanation as to why the ValueError was raised!
  • Alejandro C De Baca
    Alejandro C De Baca about 4 years
    NB: The parenthesis in the second expression are important.
  • Harsh Gupta
    Harsh Gupta almost 3 years
    will it works for date also ? 'df['date'].between(2010-03-01, 2010-05-01, inclusive=False)' I found the sol stackoverflow.com/a/29370182/8927035