numpy - User defined filter for pandas dataframe -



numpy - User defined filter for pandas dataframe -

iam writing qt based application showing tabular data. app uses pandas dataframes store information. user should able filter dataframes, e.x.:

df = pandas.dataframe({ 'elevation': [10, 20, 15, 12, 100, 150, 200, 200], 'name': ['tree', 'tree', 'house', 'tree', 'house'] }) df[(elevation > 10) & (elevation < 200)] df[(elevation > 10) & (elevation < 200) & (name == 'tree')]

how build such filter functions text input? tried utilize sympy convert function text input , lambdify later.

expr = sympify("(x > 10) & (x < 200)") f = lambdify(x, expr, "numpy") f(df)

if utilize dataframe input got error "the truth value of series ambiguous. utilize a.empty, a.bool(), a.item(), a.any() or a.all()". if utilize df.any() got true or false back, no index series... simple x > 10 expressions it's working expected, or improve said, wanted. suggestions?

in situation might able leverage query:

>>> df.query("(elevation > 10) & (elevation < 200)") elevation name 1 20 tree 2 15 house 3 12 tree 4 100 house 5 150 tree >>> df.query("(elevation > 10) & (elevation < 200) & (name == 'tree')") elevation name 1 20 tree 3 12 tree 5 150 tree

query can't handle everything, can handle stuff simple. if need more sophisticated, utilize exec or eval build functions on fly; there obvious security hazards there, you'd have same issues using sympy (it uses eval too.)

alternatively, implement own parser need.

numpy pandas filtering logical-operators sympy

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -