mardi 7 avril 2020

How do I strip the "$" symbol from a string in a pandas series?

In need of dire help. I am trying to conditionally iterate over the rows of a Google play store csv file. I keep running to the issue of pandas not recognizing the '>=' symbol during some of my loops for some reason. That said using the condition "if price == "9.00" works just fine but the other operations (i.e, '<=' and ">=" return error messages.

Additionally, I'm trying to count the number of apps with a price of $9.00 or greater. I want to strip the "$" symbol from the price column and then proceed to iterate over it. I have attempted the str.lstrip function with no success. Any and all help is greatly appreciated.


df = pd.read_csv("googleplaystore.csv")

df['Rating'].fillna(value = '0.0', inplace = True)

# Calculating how many apps have a price of $9.00 or greater

apps_morethan9 = 0

for i, row in df.iterrows():
    rating = float(row.Rating)
    price = float(row.Price)
    if price >= 9:
        apps_morethan9 += 1

print(apps_morethan9)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-103-66171ce7efb6> in <module>
      5 for i, row in df.iterrows():
      6     rating = float(row.Rating)
----> 7     price = float(row.Price)
      8     if price >= 9:
      9         apps_morethan9 += 1

ValueError: could not convert string to float: '$4.99'``` 

Aucun commentaire:

Enregistrer un commentaire