def detection_error(element):
try:
float(element)
except ValueError:
return element
print(df["Annual Salary"].apply(detection_error).loc[df["Annual Salary"].apply(detection_error).notna()].values)
无法理解它如何从数据框中获取每个元素并检查它
注意
return
函数的try
块中没有detection_error()
语句。因此,如果将 element
转换为浮点数的尝试成功,该函数将返回 None
。否则它返回元素不变。
如果函数应用于数据框中的元素,则可以过滤函数无法使用
notna()
转换的元素,因为它们不是None
.
对于数字列,pandas 会将
None
值存储为 np.nan
,但这不会改变 notna()
的结果。