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

Finding Mean Value Pandas While Having String

I have a 20*5 data table and I want to find the mean value of one of the columns which is the price column. I know I have to use this method for finding the mean value

mean= df["price"].mean()

the problem is that in my data file, the prices are not integers and they are strings and all of them are written in this format e.x.(2000dollars not just 2000). How can I remove those currencies from the numbers and then finding the mean value of the numbers?(The currencies are all the same)

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

>Solution :

Try normalising you data first,

df["price"] = df["price"].apply(lambda x: float(x.replace("dollars", "")))

I am assuming the prices are stored like 5000dollars, if you have any other abnormality, you can replace it with empty value.

or if you dont wanna update this columns entry, you can make a new column and use that one for mean,

df["new_price"] = df["price"].apply(lambda x: float(x.replace("dollars", "")))
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