Skip to content
Advertisement

Alternative to the pandas negation operator

I’m trying to use the pandas negation operator ~ in one of my jinja2 templates but I believe its conflicting with their special operator ~.

{% for row in df.loc[ ~(df['is_test_result_pass']) , : ].itertuples() %}

yields the following exception…

jinja2.exceptions.TemplateSyntaxError, unexpected '~'

I could do the operation on the python side and pass another variable with the negated selection but what’s the method name equivalent that the ~ operator maps to that I could invoke in the template.

Advertisement

Answer

Looks like even more than the ~ operator choked, I couldn’t use the column slice operator either :, it would yield a SyntaxError: invalid syntax when the template compiled

However both the following worked identically using the tip from @piRSquared….

{% for row in df.loc[ df['is_test_result_pass'].eq(False) ].itertuples() %}
{% for row in df.loc[ df['is_test_result_pass'].__neg__() ].itertuples() %}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement