Home > Mobile >  Convert a string that contains a price with space after thousands to float in pandas column
Convert a string that contains a price with space after thousands to float in pandas column

Time:02-17

I have a column that contains prices with a currency. I want to convert these prices to floats. The issue here, is that these prices contain spaces after thousands.

My initial dataframe :

import numpy as np
import pandas as pd
prices = pd.Series(['239,00$','1 345,00$','1,00$','4 344,33$'])
df = pd.DataFrame(prices,columns = ["prices"])

print df


   prices
0     239,00$
1     1 345,00$
2     1,00$
3     4 344,33$

The output I want to get is a dataframe column where my values are float and don't have spaces:

   prices
0     239.00
1     1345.00
2     1.00
3     4344.33

I tried using replace function to remove space in string but it doesn't seem to work. Any idea on how I can reach that result ?

CodePudding user response:

Remove characters that's not a digit or comma ([^\d,]) and then replace , with .:

df.prices.str.replace('[^\d,]', '').str.replace(',', '.').astype(float)

0     239.00
1    1345.00
2       1.00
3    4344.33
Name: prices, dtype: float64
  • Related