python - pandas 否定运算符的替代方案

标签 python pandas jinja2

我正在尝试在我的 jinja2 模板之一中使用 pandas 否定运算符 ~,但我相信它与他们的 special operator 冲突。 ~

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

产生以下异常...

jinja2.exceptions.TemplateSyntaxError, unexpected '~'

我可以在 python 端执行该操作,并通过否定选择传递另一个变量,但是 ~ 运算符映射到我可以在模板中调用的等效方法名称是什么。

最佳答案

使用numpy.logical_not :

df = pd.DataFrame({'is_test_result_pass':[True, False, False, True]})

print (df['is_test_result_pass'])
0     True
1    False
2    False
3     True
Name: is_test_result_pass, dtype: bool

print (np.logical_not(df['is_test_result_pass']))
0    False
1     True
2     True
3    False
Name: is_test_result_pass, dtype: bool

关于python - pandas 否定运算符的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51733792/

相关文章:

python - 基于 Python 列表从 yaml 文件中检索数据

python - 如何合并具有交集的集合(连通分量算法)?

python - 无法使用 Pandas Python 访问 excel 文件

python - Flask Jinja 路线

html - 在下拉菜单中选择不下拉

python - 新的 Dataframe 列作为其他行的通用函数 (pandas)

Python rpyc "socket.error: [Errno 113] No route to host"

python - 取组中的最大值,忽略当前行的值

python - 具有自定义函数的 DataFrame 中的聚合列表列

python - 我可以将一条路线用于多种功能吗?