5

First I build a new DataFrame frame. Then create a new frame2 by filtering some data from frame. Now I want to assign some value to frame2:

import numpy as np
from pandas import DataFrame

frame = DataFrame(np.arange(9).reshape((3, 3)), index=['a', 'c', 'd'], columns=['Ohio', 'Texas', 'California'])
mask = frame['Texas'] > 1
print frame[mask]
frame2 = frame.loc[mask]
frame2.loc['c', 'Ohio'] = 'me'
print frame2

but I got this warning:

C:\Python27\lib\site-packages\pandas\core\indexing.py:461: 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

why I keep got this warning although I used the recommended .loc syntax? what i supposed to do to avoid this warning?

Osora
  • 485
  • 1
  • 5
  • 8
  • it seems that i have found one possible solution at: http://stackoverflow.com/questions/23688307/settingwithcopywarning-even-when-using-loc. does someone have other good ideas? – Osora Mar 17 '16 at 12:47
  • What is your intention here? do you want to update a copy or the original df – EdChum Mar 17 '16 at 12:51
  • @EdChum i want to update the copy. – Osora Mar 17 '16 at 13:15

1 Answers1

4

Changing

frame2 = frame.loc[mask]

to

frame2 = frame.loc[mask].copy()

eliminates this warning.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185