Tuesday, 15 September 2015

python - Filtering a DataFrame based on multiple column criteria -



python - Filtering a DataFrame based on multiple column criteria -

how filter dataframe using 'and' ? in other words, if have dataframe named m , has columns a,b,c,d,e, how homecoming rows values in column b greater 120 , vales in column c = 7.3 ?

i tried i'm getting error:

print(m[m['b'] >120, m['c'] ==7.3])

to expand on gobrewers14's answer, need wrap around parenthesis overcome order of evaluation of operators in python.

for example, next statement fails:

in [3]: 1 > 0 & 'a' < 'b' --------------------------------------------------------------------------- typeerror traceback (most recent phone call last) <ipython-input-3-5d58a7b0bade> in <module>() ----> 1 1 > 0 & 'a' < 'b' typeerror: unsupported operand type(s) &: 'int' , 'str'

because python evaluates first 0 & 'a'. that's why need wrap statements parenthesis create sense:

in [4]: (1 > 0) & ('a' < 'b') out[4]: true

in short, looking for:

m[(m['b'] > 120) & (m['c'] == 7.3)]

python pandas

No comments:

Post a Comment