Search code examples
pythonpandasdataframemulti-index

Changing Values in multiindex pandas dataframe


I have loaded a multiindex matrix from excel to a panda dataframe.

    df=pd.read_excel("filename.xlsx",sheet_name=sheet,header=1,index_col=[0,1,2],usecols="B:AZ")

The dataframe has three index colums and one header, so it has 4 indices. Part of the dataframe looks like this:

picture of part of dataframe

When I want to show a particular value, it works like this:

df[index1][index2][index3][index4]

I now want to change certain values in the dataframe. After searching through different forums for a while, it seemed like df.at would be the right method to do this. So I tried this:

df.at[index1,index2,index3,index4]=10 (or any random value)

but I got a ValueError: Not enough indexers for scalar access (setting)!. I also tried different orders for the indices inside the df.at-Brackets.

Any help on this would be much appreciated!


Solution

  • It seems you need something like that:

    df.loc[('Kosten','LKW','Sofia'),'Ruse']=10