假设我有一个名为“quantity”的列;
Quantity
1
2
3
Plan
3
2我想将字符串Plan替换为NaN,如何在pandas中实现?
谢谢你
发布于 2021-01-25 13:46:41
如果需要,请将所有非数字值替换为non,如果需要,还可以将值转换为数字型,使用带有errors='coerce'的to_numeric
df['Quantity'] = pd.to_numeric(df['Quantity'], errors='coerce')如果需要,仅替换字符串Plan
df['Quantity'] = df['Quantity'].replace('Plan', np.nan)另外,如果需要转换为数字:
df['Quantity'] = df['Quantity'].replace('Plan', np.nan).astype(float)https://stackoverflow.com/questions/65879536
复制相似问题