Home > Back-end >  Some values turning to NAN after applying pd.to_numeric
Some values turning to NAN after applying pd.to_numeric

Time:09-23

Original data_set_exampleenter image description here

using this command:

pd.to_numeric(
df_rental['by_day'].str.replace('$', ''), errors='coerce')

Some values in the select column are turning nan using the above command with or without `.str.replace('$', '')

How should I change '-' to zero and basically change the displayed columns from object to numeric.

CodePudding user response:

Here're my take:

  • $ is a special regex character indicating end-of-string, you need .str.replace('\$', '').
  • The errors='coerce' option will replace - with nan. If you want, you can .fillna(0) after conversion; or you can do .str.replace('\$','').replace('-', '0') before conversion.
  • Also, please do not include your data as images, include as text.
  • Related