简体   繁体   English

更改多索引熊猫数据框中的值

[英]Changing Values in multiindex pandas dataframe

I have loaded a multiindex matrix from excel to a panda dataframe.我已将多索引矩阵从 excel 加载到熊猫数据框。

    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.数据帧有 3 个索引列和 1 个标题,因此它有 4 个索引。 Part of the dataframe looks like this:部分数据帧如下所示:

部分数据框的图片

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.在不同的论坛上搜索了一段时间后,似乎 df.at 将是执行此操作的正确方法。 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)!但我得到了一个ValueError: Not enough indexers for scalar access (setting)! . . I also tried different orders for the indices inside the df.at-Brackets.我还为 df.at-Brackets 中的索引尝试了不同的顺序。

Any help on this would be much appreciated!对此的任何帮助将不胜感激!

看来你需要这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM