【发布时间】:2022-07-11 23:23:03
【问题描述】:
我有一个看起来有点像这样的数据框:
offer | type
------|-----
123 | A
456 | B
789 | C
我想设置一个 if 语句,如果 type 列中包含除 A 或 B 之外的任何值,它会打印一条警告消息。值可以是大写或小写,但只能是 A 或 B。
我尝试使用下面的代码,但它不起作用 - 它返回消息说一切正常,无论类型列中是否有其他类型:
if ~df["type"].isin(["A","B","a","b"]).any():
print("WARNING - Not all offers are the correct types!")
else:
print("OK - All offers are the correct types.")
请问有人知道我哪里出错了吗?
【问题讨论】:
-
逻辑上你应该使用
all()而不是any()。