First post! I'm stuck: Changes to values in "odometer" and "price" columns aren't saving to dataframe?

I’m doing my best to ask my question in a clear manner. This is my first question. Thanks for any replies!

Screen Link:
https://app.dataquest.io/c/54/m/294/guided-project%3A-exploring-ebay-car-sales-data/3/initial-exploration-and-cleaning

My Code:


autos["price"].str.replace('$','').str.replace(',','').astype(int)
autos["odometer"].str.replace('km','').str.replace(',','').astype(int)


What actually happened, and also what I expected to happen:

0        150000
1        150000
2         70000
3         70000
4        150000
          ...  
49995    100000
49996    150000
49997      5000
49998     40000
49999    150000
Name: odometer, Length: 50000, dtype: int64

This question is in regards to exercise 294, the guided project on eBay cars.

I’m using Jupyter notebook. The output above is what I expected, but here is the problem:

When I examine the dataframe again with autos.head(), the odometer and price columns are NOT updated. They are still the original strings with dollar signs, commas, and “km.” I thought the code above would replace values in the dataframe, but that doesn’t seem to be happening.

I went back to the last practice exercise but can’t figure out where I went wrong. Am I missing any code like inplace=True somewhere? Thanks so much for any feedback! :slightly_smiling_face:

Click here to open the screen in a new tab.

I believe you need to assign your changes to a variable. Currently you’re telling Python to make those changes, but you’re not telling Python where to keep them, so each time you call that dataframe, it’s not going to have the changes in it.

Like this:

autos['price'] = (autos['price'].str.replace('$','').str.replace(',','').astype(int))
3 Likes

Hi Noah, you were exactly right, this resolved my problem. Thanks so much!

1 Like

Note you can also chain the 2 replace steps into one because we can use an OR in replace e.g.

autos['odometer_km'] = autos['odometer_km'].str.replace('km|,' , '')

The | in this case stands for OR