You used the wrong notation. Try
data.ix['bar','c1'] = -999
The first element of the index refers to rows, and the second to columns. See the Docs.
Question
I want to make a conditional replacement based on the first index value in my pandas dataframe. If I have a dataframe such as:
from pandas import *
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = zip(*arrays)
index = MultiIndex.from_tuples(tuples, names=['first','second'])
data = DataFrame(randn(8,2),index=index,columns=['c1','c2'])
I think I should be able to replace value in column via:
data.ix['bar']['c1'] = -999
But this returns the original dataframe, unchanged. Can anyone explain how this should be done and why my current method does not work?
Solution 2
You used the wrong notation. Try
data.ix['bar','c1'] = -999
The first element of the index refers to rows, and the second to columns. See the Docs.
OTHER TIPS
You could use .loc
:
>>> data.loc["bar", "c1"]
second
one 0.369406
two 0.691445
Name: c1, dtype: float64
>>> data.loc["bar", "c1"] = -999
>>> data
c1 c2
first second
bar one -999.000000 0.302155
two -999.000000 -1.260789
baz one 0.103455 -0.556967
two -1.600112 0.491787
foo one 0.779901 -0.885565
two -1.041722 -0.570302
qux one -1.152093 -1.767028
two -0.364624 -0.302240
[8 rows x 2 columns]
maybe this:
data.c1[ 'bar' ] = -999
or
data[ 'c1' ][ 'bar' ] = -999
my guess is that in here data.ix['bar']['c1']
is returning a copy as opposed to view. see this