I'd like to change a value in my pandas dataframe, and I think I have misunderstood how the indexing works.
import pandas as pd
idx = pd.IndexSlice
df.loc[idx[(0, 2006.0, '01019_13055_01073_01009_01055')],idx[('moment_25','P517')]]
I get the output
Out[376]:
moment_25 P517 0.665873
Name: (0, 2006.0, 01019_13055_01073_01009_01055), dtype: float64
I'd like to change the value 0.665873 in df to 1. I've tried
df.ix[idx[(0, 2006.0,'01019_13055_01073_01009_01055')],idx[('moment_25','P517')]]=1
but I get the error
Exception: cannot handle a non-unique multi-index!
I've tried to duplicate the problem with a sample dataframe to no avail.
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.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)
df.loc[idx['A'],idx[('baz','one')]]
Out[391]: -0.17935592549360641
I think the issue is that I'm getting a series for my output when I'm using my actual data, but I get a float when I use the practice data. Why am I getting that series instead of the float 0.665873?