There are already a couple of SO questions related to this, especially this one , however, none of the answers work for me and quite a few links to documents (especially lexsorting) are broken, so I will ask for another one.
I am trying to do something (seemingly) very simply. Consider the following multi-indexed Dataframe:
import pandas as pd; import random
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
df = pd.concat([pd.Series(np.random.randn(8), index=index), pd.Series(np.random.randn(8), index=index)], axis=1)
Now I want to set all the values in the column 0for some value (for example, np.NaN) for observations in the category one. I did not succeed:
df.loc(axis=0)[:, "one"][0] = 1 # setting with copy warning
and
df.loc(axis=0)[:, "one", 0] = 1
which either issues a warning about the length of keys exceeding the length of the index, or one about the lack of lexsorting at sufficient depth.
What is the right way to do this?