简体   繁体   English

pandas 不会重命名多索引列名

[英]pandas won't rename multiindex column name

I've just spent several hours trying to get this to work and I'm starting to think I want the impossible, though I'm pretty sure it can be done.我刚刚花了几个小时试图让它工作,我开始认为我想要不可能的事情,尽管我很确定它可以完成。 I have a pandas dataframe which has a multiindex header (excel spreadsheet, 3 rows header).我有一个 pandas dataframe 它有一个多索引 header (excel 电子表格,3 行标题)。 I am definitely looking at it, so I know it exists, yet when I try to rename a column according to the official documentation, I'm told the column name can't be found.我肯定在看它,所以我知道它存在,但是当我尝试根据官方文档重命名列时,我被告知找不到列名。

The table looks like this:该表如下所示:

Test              | Test1         | Test2
                  | abc   | xyz   | abc   | xyz
geo1    | geo2    | geo1  | geo2  | geo1  | geo2
------------------------------------------------
a       | x       | 1     | 0.5   | 1     | 0.5
b       | y       | 2     | 0.2   | 2     | 0.2
c       | z       | 3     | 0.4   | 3     | 0.3

I simply want to change "Test" into "Boom", for example.例如,我只是想将“Test”更改为“Boom”。 Test is the first value of the column names in level 0, yet it doesn't work. test 是级别 0 中列名的第一个值,但它不起作用。 I used one of these:我使用了其中之一:

df.rename(columns={df.columns[0][0]: 'Boom'}, inplace=True, errors='raise')
df.rename(columns={df.columns[0][0]: 'Boom'}, level=0, inplace=True, errors='raise')
df.rename(columns={df.columns.values[0][0]: 'Boom'}, inplace=True, errors='raise')

Problem is, even if I hard code the column names and level, it still doesn't work, This should do the trick as it works in other scripts of mine (2 levels: not 3):问题是,即使我对列名和级别进行硬编码,它仍然不起作用,这应该可以解决问题,因为它适用于我的其他脚本(2 个级别:不是 3 个):

df.rename(columns={'Test': 'Boom'}, level=0, inplace=True, errors='raise')

The error is funny, as it's telling me it can't find the "Test" column (it's literally telling me it can't find the column it just names...).这个错误很有趣,因为它告诉我它找不到“测试”列(它实际上是告诉我它找不到它刚刚命名的列......)。 What am I doing wrong??我究竟做错了什么??

Thank you all!谢谢你们!

df.columns.set_levels(['Boom1','Boom2','Boom3'],level=0,inplace=True)

If your columns are Boom1, ..., Boom1000, first create a list of names by如果您的列是 Boom1,...,Boom1000,首先创建一个名称列表

ll = [f"Boom{i}" for i in range(1,1001)]
df.columns.set_levels(ll,level=0,inplace=True)

I just removed errors='raise' from the function and it worked.我刚刚从 function 中删除了 errors='raise' 并且它起作用了。 there's no logic in the way pandas works, but this seems to have done the trick. pandas 的工作方式没有逻辑,但这似乎已经成功了。 not sure how something can work, but if you tell it to raise an error if needed, it stops working.不确定某些东西是如何工作的,但如果你告诉它在需要时引发错误,它就会停止工作。 thanks all for trying anyway.无论如何,感谢所有人的尝试。 if someone could explain why this is the way it is, for my own sanity, I'd appreciate it!如果有人能解释为什么会这样,为了我自己的理智,我会很感激的!

This combination of parameters works for me:这种参数组合对我有用:

index = pandas.MultiIndex.from_tuples([('A', 'X'), ('B', 'Y'), ('C', 'Z')], names=['id1', 'id2'])
columns = pandas.MultiIndex.from_tuples([('Test1', 'a', 'x')], names=['col1', 'col2', 'col3'])

df = pandas.DataFrame(
    data = [1, 2, 3],
    index=index,
    columns=columns
)

df.rename(columns={'Test1': 'Boom!'}, level='col1')

which return a new dataframe:它返回一个新的 dataframe:

col1    Boom!
col2        a
col3        x
id1 id2      
A   X       1
B   Y       2
C   Z       3

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

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