Ebay Car Sales Data guided Project: Getting "SettingWithCopyWarning" for setting column values

I am working on the guided project based on ‘Ebay Car Sales Data’ from Germany.

I am having trouble regarding setting the rows of the columns with new values.
In my project work:

  1. In the cell no-123, I am changing the columns with German values to their English counter parts. For example in the column ‘seller’, the initial value is ‘privat’ and I am changing it to ‘private’.

While trying to change them using the method :-

1. autos.loc[:,"seller"] = autos.loc[:,"seller"].map(map_seller).copy()

or

2.  autos.loc[:,"seller"].replace(map_seller, inplace = True)

I am getting the “SettingWithCopyWarning” with the following message:

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/indexing.py:966: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self.obj[item] = s

I am also getting the same warning in cell no-124 when converting the date columns to be uniform numeric data, so "2016-03-21" becomes the integer 20160321.

Basics.ipynb (137.7 KB)

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

1 Like

Hi @asim.choudhury! That’s a typical warning that happens in most data-cleaning operations. I believe that in your case, the problem is that pandas does not know if you are modified a view (i.e., a subset of your data) or a copy of a subset of the data. Try writing .copy() before you .map(). This should solve the issue. Let me know if it worked.

For a deeper understanding of what’s happening, check out this article.

Happy coding :slight_smile:

3 Likes

Hi @artur.sannikov96

Thank You for your kind answer. Somebody from the community already helped me to find this solution as I had posted the question in ‘Q&A’ section before posting here. Sorry, I did not know the rules and then they guided me to post the project here.

Yes, I stopped receiving the warning after I used .copy().

Grateful for the helpful community.

2 Likes