python - Pandas 值(value)错误: invalid literal for int() with base 10: ''

标签 python python-3.x pandas dataframe

我有一个df

inv_id
d5
dd

a123

我喜欢将 inv_id 转换为 int 并检查其值是否 < 100,

df['inv_id'].str.replace(r'\D+', '').apply(int) < 100

但是我得到了

ValueError: invalid literal for int() with base 10: ''

我知道空字符串不能转换为int,但我喜欢将空字符串设置为False,无论inv_id是空字符串最初或在 str.replace 转换为空字符串之后。所以结果看起来像,

inv_id    res
d5        True
dd        False
          False
a123      False

最佳答案

您可能需要使用 to_numeric 而不是 apply(int) ..

pd.to_numeric(df.inv_id.str.replace(r'\D+', ''),errors ='coerce')<100

关于python - Pandas 值(value)错误: invalid literal for int() with base 10: '' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54869213/

相关文章:

python - NumPy 的 NDPointer : Don't know how to convert parameter 1

python - 日期分类

python - 如何使用python从视频中提取幻灯片

python - PyQt 保持纵横比固定

python - (Python,数据帧): Record the average of all numbers in a column that are smaller than the n'th percentile

python - 如何在一列中找到三个输入最多的值?

java - 泛美卫生组织客户限制?

使用 types.FunctionType 时的 Python 版本问题

python - virtualenvs 应该去哪里生产?

python - 用 pandas 和 numpy Python 替换列表推导式