Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

How to select rows in a DataFrame between two values, in Python Pandas?

df = df[df['closing_price'].between(99, 101)]
#OR
df = (
      df[
        (df['closing_price'] >= 99) & 
        (df['closing_price'] <= 101)
        ]
     )
#OR
df.query('99 <= closing_price <= 101')
#OR
 df.query('closing_price.between(99, 101, inclusive=True)', engine="python") 
#- but this will be slower compared to "numexpr" engine.
#OR
newdf = df.query('closing_price.mean() <= closing_price <= closing_price.std()')
#OR
mean = closing_price.mean()
std = closing_price.std()
newdf = df.query('@mean <= closing_price <= @std')
#OR
df = df[(99 <= df['closing_price'] <= 101)]


Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #select #rows #DataFrame #Python
ADD COMMENT
Topic
Name
7+5 =