Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   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 ?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading