0

Suppose a pandas dataframe

d = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6], 'c':[7,8,9]}) 

How can I select all the rows such that 'a'>1, 'b'<6 and 7<='c'<=9 ?

It should result in this case equivalent to the second row of the dataframe. In the solutions suppose a generic dataframe with k keys.

2 Answers 2

2

You could use query

In [233]: d.query('a>1 and b<6 and 7<=c<=9') Out[233]: a b c 1 2 5 8 

Also, you could do

In [234]: d[(d.a>1) & (d.b<6) & (d.c>=7) & (d.c<=9)] Out[234]: a b c 1 2 5 8 

And, pd.eval() works well with expressions containing large arrays

In [235]: d[pd.eval('(d.a>1) & (d.b<6) & (d.c>=7) & (d.c<=9)')] Out[235]: a b c 1 2 5 8 
Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot for the answer but I am still having problems. I have edited the question
Could paste few rows of data/frame?
There is a comma in (dta.age3 >= low_age), (dta.bmi3>= low_bmi) & instead of & ?
1

You can directly index the dataframe, putting all conditions inside parentheses:

d[(d.a > 1) & (d.b < 6) & (d.c <= 9) & (d.c >= 7)] 

For 'or' conditions, use pipe | between conditions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.