dict to boolean expression, how to?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Aug 1 11:24:49 EDT 2014
On Fri, 01 Aug 2014 14:26:38 +0000, Alex van der Spek wrote:
[...]
> bool = ((df['a'] == 1) & (df['A'] == 0) |
> (df['b'] == 1) & (df['B'] == 0) |
> (df['c'] == 1) & (df['C'] == 0))
>
> I do know eval() lends itself to code injection but can't say I am fully
> aware of its dangers. It seemed like a handy tool to me.
And in expert hands, it can be. But I can't see how you're using eval in
this case. Since you're not having any success, perhaps it's not as handy
as you imagine?
Did you try the suggestion to use any( ... )? Did it work?
> This newsgroup scares me, it appears to be for professional computer
> scientists only,
Not even close! Some of us aren't even professional programmers!
> the theoretical part is sometimes too much for this
> practical physicist with an old background in FORTRAN.
Oh, I sympathise. I know it can be hard to avoid feeling overwhelmed when
you are dropped far outside of your comfort zone in a topic you know
nothing about. But you're a physicist, and therefore you've already
proven you have the logical skills to reason about quite complex and
mathematical topics. If you're lost now, I wager it's because you're
unfamiliar with the territory, not because it's beyond you.
So please, don't be shy about asking people to explain in simpler terms.
Sometimes we forget that things which are second nature to us are not
familiar to somebody new to the language. Or we don't want to patronise
them by assuming that every person asking a question needs their hand-
held and every little detail spelled out in painfully small steps. It's
quite hard to find the right balance, especially when we don't know your
level of knowledge.
So please, ask concrete questions, the more specific the better. Don't be
afraid to ask what unfamiliar terms means.
> Is there a better place to ask questions of this nature?
I don't know of anyone here who is an expert in pandas, so if you ask
questions which are specific to pandas, we may run into the limits of our
knowledge. If you can find a dedicated pandas mailing list or other
forum, they may help too, but I don't know if they will be more or less
willing to explain the basics about Python.
--
Steven
More information about the Python-list
mailing list