How to replace values in a column?

Screen Link: https://app.dataquest.io/m/294/guided-project%3A-exploring-ebay-car-sales-data/9/next-steps

Your Code:

autos.loc[:,['vehicle_type']].replace(
    ['bus', 'limousine', 'kleinwagen', 'kombi', 'coupe', 'suv','cabrio', 'andere'],
    ['bus', 'limousine', 'compact', 'station_wagon', 'coupe', 'suv', 'convertible', 'other'],
    inplace = True
    )

autos.loc[:, ['gearbox']].replace(
    ['manuell', 'automatik'],
    ['manual','automatic'],
    inplace = True
    )

autos.loc[:,['fuel_type']].replace(
    ['lpg', 'benzin', 'diesel', 'cng', 'hybrid', 'elektro', 'andere'],
    ['lpg', 'gasoline', 'diesal', 'cng', 'hybrid', 'electric', 'other'],
    inplace = True
    ) 

What I expected to happen: Replace the german words with the english words.

What actually happened: German words are unchanged.

Other details: I used this link for help on how to replace values in a column. The whole reason I’m going down this road is to avoid setting with copy warnings. Any clarity on this issue would be amazing.

1 Like

Hi @lockwood.lance,

Please try this code:

autos.loc[:,'vehicle_type'].replace(
    ['bus', 'limousine', 'kleinwagen', 'kombi', 'coupe', 'suv','cabrio', 'andere'],
    ['bus', 'limousine', 'compact', 'station_wagon', 'coupe', 'suv', 'convertible', 'other'],
    inplace = True
    )

Let me know how that goes.

Best,
Sahil

1 Like

Hi @Sahil,
I tried it and got this error:
/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/generic.py:4619: SettingWithCopyWarning:

A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

Any thoughts?

Hi @lockwood.lance,

I would recommend you to check out this blog post:

If that didn’t help, please upload your jupyter notebook (.ipynb) file. I will check it on my end.

Best,
Sahil

Hey,

I think a way better way of dealing with the translation is to create a dictionary with the german words and their english translations and then use the map function, e.g.:

map_dict = { “manuell”: “manual”, “automatik”: “automatic”}

autos.gearbox = autos.gearbox.map(map_dict)

Cheers,
Jonas

2 Likes

Hi @lockwood.lance, did you fix the problem? I am having a similar issues with replacing the value names, I have checked the blog post on ​​​​SettingwithCopyWarning which was suggested, but it did not help. Let me know if you found any solutions to it, it might help me as well :blush:
This is the code that does not work, it does not replace the values.
autos.loc[:,['vehicle_type', 'gearbox', 'fuel_type', 'model', 'unrepaired_damage', 'brand']].replace({"kleinwagen": "super mini", "kombi":"station wagon", "cabrio": "convertible", "limousine": "sedan", "andere": "other", "manuell": "manual", "automatik": "automatic", "benzin":"gas", "elektro": "electric", "sonstige_autos": "other", "nein": "no", "ja": "yes"}, inplace=True)

@annalisa, @Sahil, @jonas.wordman
Sorry crew for the late reply. This one has been bugging me for awhile, even when moving on to the next exercises. The issue is Data Quest not our code. Downloaded the autos dataset and loaded into a data frame on my computer because after trying everything and looking up answers on stack overflow I still got an copy warning. The .replace and the .map both worked with out copy setting warnings. Super frustrating that we had this right in the beginning. Attached is my notebook the verifies both methods. In summary though it looks like:
autos.seller = autos.seller.map({'privat':'private', 'gewerblich':'commercial'})
or
autos.loc[:,'seller'].replace(['privat','geverblich'],['private','commercial'], inplace = True)
@Sahil Do you know if there is there an update in the works for the code?
P.S. Showing the shorter translation for seller because it’s compact but gets the point across.
autos_cleanup.ipynb (16.6 KB)

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

1 Like

Hi @lockwood.lance,

I have tried running your notebook file in Dataquest and I haven’t received any SettingWithCopyWarning.

@annalisa, @lockwood.lance,

Can you please attach the original notebook files on which you were getting these warnings? I will check it on my end.

Best,
Sahil

autos_guided.ipynb (382.7 KB)
@Sahil,
Here’s my file. If you run it locally I get the feeling it will work fine because it will use your machines current version of python. My hunch is it lies on the hosted version of jupyter notebook. Attached is a screenshot of the error in the module.



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

Hi @lockwood.lance,

Version differences can make a difference in the results. That is especially applicable to warnings like SettingwithCopyWarning, which can even trigger for false-positive cases.

False positives, or situations where chained assignment is inadvertently reported, used to be more common in earlier versions of pandas but have since been mostly ironed out. For completeness, it’s useful to include some examples here of fixed false positives. If you experience any of the situations below with earlier versions of pandas, then the warning can safely be ignored or suppressed (or avoided altogether by upgrading!)…
https://www.dataquest.io/blog/settingwithcopywarning/

But concluding it as a false positive case should be done after checking it very thoroughly. So, let’s skip that possibility for now. After running your code on both Dataquest (Pandas version 0.22.0) and my local environment (Pandas version 1.0.1), I received the SettingwithCopyWarning on autos.loc[:,'seller'].replace(['privat','geverblich'],['private','commercial'], inplace = True).

As there is nothing wrong with this line, this may be a false positive case. Additionally, the possibility only increases when we cannot reproduce this warning in a different notebook that attempts to do the same.

However, just because the warning got triggered on autos.loc[:,'seller'].replace(['privat','geverblich'],['private','commercial'], inplace = True), it doesn’t necessarily mean something is wrong with that line. The issue might be due to the codes in the upper cells. In this case, it is caused by this line of code:

autos = autos[
    (autos['registration_year'] >= 1900) &
    (autos['registration_year'] <= 2016)
             ]

When we try to subset a dataframe, it returns a view instead of a copy. So the warning in your case can be solved by adding a .copy() method to the end like this:

autos = autos[
    (autos['registration_year'] >= 1900) &
    (autos['registration_year'] <= 2016)
             ].copy()

After that, try running your code from start to finish. You will not receive the warning this time.

Best,
Sahil

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