I have a (possibly large) DataFrame
and I want to set values based on multiple criteria.
I would like to do this by first applying the "big cut" once, and then changing values on parts of that slice. But I want these changes to apply to the original DataFrame
, so I don't want a copy when I do the first selection.
Here is a simple example.
import pandas
df = pandas.DataFrame( [(1,2,3),
(4,5,6),
(7,8,9)],
columns=["x","y","z"]
)
# the "big cut" -- does this return a copy? why?
vw = df.loc[ df["x"]<5, : ]
# the "sub" selections set values only in vw, not df
vw.loc[ vw["z"]>5, "z" ] = 666
vw.loc[ vw["y"]<5, "y" ] = 222
print "vw:"
print vw
print "df:"
print df
which prints the result:
vw:
x y z
0 1 222 3
1 4 5 666
df:
x y z
0 1 2 3
1 4 5 6
2 7 8 9
My desired result:
df_ideal:
x y z
0 1 222 3
1 4 5 666
2 7 8 9
How can I do my sub-selections on a view rather than a copy? I have read this question, but it seems like I should be getting a view by those rules. (I am using Pandas 0.19.2 and Python 2.7.10)