user308827
user308827

Reputation: 21961

Getting settingwithcopywarning error even after using .loc

I get this error:

C:\Users\rt\Anaconda3\lib\site-packages\pandas\core\indexing.py:337: 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: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self.obj[key] = _infer_fill_value(value)
C:\Users\rt\Anaconda3\lib\site-packages\pandas\core\indexing.py:517: 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: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self.obj[item] = s

However, I am already using .loc already so not sure why this error is occuring. The code outline is like this:

for name, group in results.groupby():
    for idx, row in group.iterrows():
        group.loc[idx, 'area'] = 10.0

I am using latest pandas (0.20.3) and python (3.6)

Upvotes: 1

Views: 252

Answers (1)

Ken Wei
Ken Wei

Reputation: 3130

It's telling you that you are modifying a copy of the dataframe you may be intending to modify, instead of the dataframe itself. In this case, group is actually another dataframe created by the .groupby() generator.

Your code is leaving results untouched, and modifying group instead. This is probably not what you intend to do, which is why the warning comes up. If it is, and you want to disable the warning, you can do pd.set_option('SettingWithCopyWarning', None).

Upvotes: 2

Related Questions