0

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

yangyang
  • 491
  • 4
  • 16
  • 4
    Does this answer your question? [How to replace a value in pandas, with NaN?](https://stackoverflow.com/questions/29247712/how-to-replace-a-value-in-pandas-with-nan) – eligolf Jan 25 '21 at 05:47

1 Answers1

1

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)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252