Supposed I have a column called "quantity";
Quantity
1
2
3
Plan
3
2
and I want to replace the string Plan
as NaN, how can I do that in pandas?
thankyou
Supposed I have a column called "quantity";
Quantity
1
2
3
Plan
3
2
and I want to replace the string Plan
as NaN, how can I do that in pandas?
thankyou
If need replace all non numeric values to NaNs and also if necessary convert values to numeric use to_numeric
with errors='coerce'
:
df['Quantity'] = pd.to_numeric(df['Quantity'], errors='coerce')
If need replace only strings Plan
:
df['Quantity'] = df['Quantity'].replace('Plan', np.nan)
Also if need convert to numeric:
df['Quantity'] = df['Quantity'].replace('Plan', np.nan).astype(float)