-2

I am trying to convert a column in my code which is currently in a string $ format to numeric.

My code is currently:

s=top_performing_schools["Per Student Budget"]

top_performing_schools["Per Student Budget"]=pd.to_numeric(s)

I get the error:

ValueError: Unable to parse string "$582.00" at position 0
ramango
  • 21
  • 1
  • 5
  • 1
    Does this answer your question? [converting currency with $ to numbers in Python pandas](https://stackoverflow.com/questions/32464280/converting-currency-with-to-numbers-in-python-pandas) – Anshu Chen Mar 17 '20 at 19:39
  • 1
    **Please provide a [mcve], as well as the entire error message.** – AMC Mar 17 '20 at 19:43

2 Answers2

1

You can delete the $ symbol and then apply the numeric conversion, for example:

import pandas as pd


df = pd.DataFrame(
    {"Per Student Budget": ["$582.00", "$100", "$12000"]}
)

df["Per Student Budget"] = pd.to_numeric(df["Per Student Budget"].str.replace('$', ''))
print(df)

   Per Student Budget
0               582.0
1               100.0
2             12000.0
marcos
  • 4,473
  • 1
  • 10
  • 24
0

import pandas as pd

df = pd.DataFrame( {"Per Student Budget": ["$582.00", "$100", "$12000"]} )

df["Per Student Budget"] = pd.to_numeric(df["Per Student Budget"].str.replace('$', '')) print(df)

Per Student Budget 0 582.0 1 100.0 2 12000.0

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 04 '22 at 22:16