I would like to modify some values from a column in my DataFrame. At the moment I have a view from select via the multi index of my original df (and modifying does change df).
Here's an example:
In [1]: arrays = [np.array(['bar', 'bar', 'baz', 'qux', 'qux', 'bar']),
np.array(['one', 'two', 'one', 'one', 'two', 'one']),
np.arange(0, 6, 1)]
In [2]: df = pd.DataFrame(randn(6, 3), index=arrays, columns=['A', 'B', 'C'])
In [3]: df
A B C
bar one 0 -0.088671 1.902021 -0.540959
two 1 0.782919 -0.733581 -0.824522
baz one 2 -0.827128 -0.849712 0.072431
qux one 3 -0.328493 1.456945 0.587793
two 4 -1.466625 0.720638 0.976438
bar one 5 -0.456558 1.163404 0.464295
I try to modify a slice of df to a scalar value:
In [4]: df.ix['bar', 'two', :]['A']
Out[4]:
1 0.782919
Name: A, dtype: float64
In [5]: df.ix['bar', 'two', :]['A'] = 9999
# df is unchanged
I really want to modify several values in the column (and since indexing returns a vector, not a scalar value, I think this would make more sense):
In [6]: df.ix['bar', 'one', :]['A'] = [999, 888]
# again df remains unchanged
I'm using pandas 0.11. Is there is a simple way to do this?
The current solution is to recreate df from a new one and modify values I want to. But it's not elegant and can be very heavy on complex dataframe. In my opinion the problem should come from .ix and .loc not returning a view but a copy.