Guided Project: Exploring eBay car sales step 4/9 - price and odometer return to object dtype

Hi all,

I have removed non-numeric characters on ‘price’ and ‘odometer’ column and change them into integer dtype with this code and all works well:

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

However, it returns to the original columns with non-numeric characters and object dtype when I typed:

autos['price'].describe

and this is my code
Basics (1).ipynb (131.0 KB)

Have you guys got any thought?
Thank you.

Warm regards,
Raden

Click here to view the jupyter notebook file in a new tab

Here’s a hint. What’s happening here is the same that is happening below.

>>> x = "3" # `x` is a string
>>> int(x) # This returns an integer
3
>>> x
'3'

At the end x was still a string.

2 Likes

Hi @Bruno,

Thank you for your reply and pointing it out.
I have revised my code to:

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

and all works well for the next code.
Thank you very much.

Warm regards,
Raden

1 Like

hi @Bruno,
please can you help me figure out am not doing right?
price and odometer_km turned back as string
thanks
Basics (4).py (2.0 KB)

Not true. Please share a notebook (not a script) where you’re observing this behavior.

hi @Bruno,
thanks for the response.
i guess i have uploaded the right file
Basics.ipynb (115.2 KB)

Click here to view the jupyter notebook file in a new tab

I don’t see the behavior you mention in that notebook. In fact, I see opposite, both columns are int64.

thanks, i will double check

Hello, can someone please tell me what I’m doing wrong at this step? I get the KeyError ‘price’. Below is the code I used:
autos_c[“price”] = autos_c[“price”].str.replace("$","").str.replace(",","").astype(int)
autos_c[“odometer”] = autos_c[“odometer”].str.replace(“km”,"").str.replace(",","").astype(int)

autos_c.rename({“price”:“price_USD”, “odometer”:“odometer_km”}, axis=1, inplace=True)

autos_c

Basics-Copy1.ipynb (188.1 KB)

Click here to view the jupyter notebook file in a new tab

I see there are quite a few cell executions that are not visible. Can you please restart your kernel, run everything and share the notebook again?

Yes, here it is below. Although, now it is not showing the error. So I guess it is fine now.

https://app.dataquest.io/jupyter/notebooks/notebook/Basics-Copy2.ipynb