I would like to modify DataFrame entries for some rows at a specific level of a hierarchical index. Here is a canonical example:
>>> index = pd.MultiIndex.from_arrays([['a','a', 'b', 'b'], [1,2,1,2]],
... names=['first', 'second'])
>>> data = pd.DataFrame(np.random.rand(len(index)), index=index, columns=['A'])
>>> print data
A
first second
a 1 0.587781
2 0.560407
b 1 0.492996
2 0.267799
I would like to set rows for which second==2 to 0 (for example). I tried using DataFrame.xs method but it returns a copy and not a view:
>>> selected = data.xs(2, level='second')
>>> print selected
A
first
a 0.560407
b 0.267799
>>> selected['A']=0
>>> print data
A
first second
a 1 0.587781
2 0.560407
b 1 0.492996
2 0.267799
The last assignment did not affect data (it changed values in selected of course).
In recent version of pandas
help(data.xs)shows how to get a view on the data.It can be done by using
xs arg copy=False.