我有一个包含两列field
和value
的数据框,正在进行对每个字段值的检查。对于field a
,我需要检查其对应的值始终为list
类型,并将结果存储在status
列中。
以下是您提供的代码:
import pandas as pd
from pandas.api.types import is_list_like
data = {
"field": ["a", "b", "c"],
"value": [[1, "na", -99], 20, 80],
}
df = pd.DataFrame(data)
print("初始数据框")
print(f"{df=}")
condlist = [df["field"] == "a", df["field"] == "b", df["field"] == "c"]
choicelist = [
df["value"].apply(is_list_like).any(),
df["value"].isin([10, 20, 30, 40]),
df["value"].between(50, 100), # 这里存在问题
]
df["status"] = np.select(condlist, choicelist, False)
print("检查后的数据框")
print(f"{df=}")
您遇到的错误是:
df["value"].between(50, 100),
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "pandas/_libs/ops.pyx", line 107, in pandas._libs.ops.scalar_compare
TypeError: '>=' not supported between instances of 'list' and 'int'
我漏掉了什么?