I am trying to replace all columns in my df with prices to ints however for some reason the replace() method isn't working:
df = pd.read_csv(f_name, dtype="string")
df = df[df.columns.dropna()]
df[cols_int] = df[cols_int].replace({"[\$]": "", "[,]": ""}, regex=True)
df[cols_int] = df[cols_int].astype(int)
Error:
ValueError: invalid literal for int() with base 10: '$499,000'
I'd appreciate any help!
As of pandas 1.3, this bug should not occur anymore.
If you are still using pandas <1.3, this bug was caused by the "string" dtype, so use dtype=str
or dtype="str"
instead:
df = pd.read_csv(f_name, dtype=str)
# ---
df = df[df.columns.dropna()]
df[cols_int] = df[cols_int].replace({"[\$]": "", "[,]": ""}, regex=True)
df[cols_int] = df[cols_int].astype(int)