Error when converting to numeric

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

My Code:

autos["price"] = autos["price"].str.replace("$","").replace(",","").astype(int)

What I expected to happen:
Converts the columns to integers

What actually happened:

ValueError: invalid literal for int() with base 10: '5,000'

I seem to have the same code as others but for some reason I am still not able to convert to integer, any ideas why?

Hi @jonathan.s.spratt,

At one point you’ve forgotten something when doing vectorized string operations.

Try looking at the result in the error, it says '5,000' that is a very big hint!

Check the starred str

autos["price"] = autos["price"].str.replace("$","").*str*.replace(",","").astype(int)

Thank you! Wasn’t aware you had to call str before each replace

1 Like

You have to use the str before every string method. You have to use dt before datetime operations.

Cheers!

Well @jonathan.s.spratt,

You do not need to use .str before every replace. Only when you are replacing a complete column. a.k.a vectorized string. For replacing one value it would throw another error. String does not have the attribute ‘str.’ Which you will most likely get at some point :wink:

Do you know Guido van Rossum?

He created the Python Language.

1 Like

I guess @monorienaghogho you’re talking to me as I am Dutch as well.

And I have to disappoint you, we are 17 million humans in the Netherlands, Guido is just one.

By the way, I would probably have learned Python a long time before if I would have.

I did the exact same thing for both odometer_km and price columns (dropped the unwanted characters and converted to an int type.

Then I did an autos.head() to check the df and it is still showing the $ sign and comma under price and km and comma under odometer_km columns.

Even worse, checking the dype following this operation still showing those column as object types. What am I doing wrong?

Hello @sajed_h

Sorry for the late reply.

Could you upload your notebook?

Thanks.

1 Like

No worries. Thanks for reaching out. Basics.ipynb (118.3 KB)

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

1 Like

@sajed_h

I think you forgot to assign back to the dataframe.

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

This worked as well:

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

1 Like

That was such a silly oversight from me!

I greatly appreciate your help. It’s wonderful to have a set of helping eyes :slight_smile:

2 Likes
autos["price_$"] = autos["price_$"].str.replace("$","").str.replace(",","").astype(int)
autos["odometer_km"] = autos["odometer_km"].str.replace("km","").astype(int)

hello here is my code, i ran it yesterday but up to it this morning it is giving error. kindly help out