Search code examples
pythonpandasdataframemulti-index

updating a value in a multi index data frame


could someone please explain this behavior to me? this is my dataframe:

df = pd.DataFrame(
    {
    'key1' : ['f1', 'f1', 'f2', 'f2', 'f3'],
    'key2' : ['fm1', 'fm2', 'fm1', 'fm2', 'fm1'],

    'k' : np.random.rand(5),
    'c' : [100, 200, 100, 150, 400],
    })
df.set_index(["key1","key2"],inplace=True)

when I try to change a value:

df.loc["f1","fm1"].loc["k"]=-999

nothing happens. and df.loc["f1","fm1"].loc["k"] still gives the old value I am wondering what I am doing wrong, and how should I achieve this?


Solution

  • Use a tuple and only one loc:

    >>> df.loc[("f1", "fm1"), "k"] = -999
    >>> df
                        k    c
    key1 key2                 
    f1   fm1  -999.000000  100
         fm2     0.082072  200
    f2   fm1     0.119572  100
         fm2     0.752586  150
    f3   fm1     0.947604  400
    >>> 
    

    The reason your code doesn't work, is because df.loc["f1", "fm1"] references to an object that has no interference with the original df, it returns a completely new dataframe and replaces the k value, but it doesn't replace it in df as well.

    Read more about it here on the pandas documentation.