1

I have a mixed bag of dollar values as strings in a column; some with $ and commas and some without. How do I convert these to integer values? Here's a sample.

df2.PRICE
Out[193]: 
0         $179,000.00
1         $110,000.00
2         $275,000.00
3         $140,000.00
4         $180,000.00
    
564611          85500
564612          80800
564613          74500
564614          75900
564615          66700
Name: PRICE, Length: 564616, dtype: object
SEU
  • 1,304
  • 4
  • 15
  • 36

1 Answers1

2

Use Series.replace with convert to floats by Series.astype:

df2.PRICE = df2.PRICE.replace('[\$,]','', regex=True).astype(float)
print (df2)
           PRICE
0       179000.0
1       110000.0
2       275000.0
3       140000.0
4       180000.0
564611   85500.0
564612   80800.0
564613   74500.0
564614   75900.0
564615   66700.0

If there are always integers:

df2.PRICE = df2.PRICE.replace('[\$,]','', regex=True).astype(float).astype(int)
print (df2)
         PRICE
0       179000
1       110000
2       275000
3       140000
4       180000
564611   85500
564612   80800
564613   74500
564614   75900
564615   66700

If failed converting to floats use to_numeric with errors='coerce' for missing values if cannot convert to number:

df2.PRICE = pd.to_numeric(df2.PRICE.replace('[\$,]','', regex=True), errors='coerce')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252