如何使用 Pandas 重命名重置索引上的多个列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27324115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:44:41  来源:igfitidea点击:

How to Rename Multiple Columns on a Reset Index with Pandas

pythonpandasrenamemultiple-columns

提问by slee

I'm trying to figure out if there is a way to rename Pandas columns when you try to reset the index. I see in the documentation that you can use the "name" parameter to set the column name of a reset index if there is only one column, but I'm curious if there is a way to do this for multiple columns.

我试图弄清楚当您尝试重置索引时是否有一种方法可以重命名 Pandas 列。我在文档中看到,如果只有一列,您可以使用“name”参数来设置重置索引的列名,但我很好奇是否有办法为多列执行此操作。

For example:

例如:

df1 = pd.DataFrame({
'A' : ['a1', 'a1', 'a2', 'a3'],
'B' : ['b1', 'b2', 'b3', 'b4'],
'D1' : [1,0,0,0],
'D2' : [0,1,1,0],
'D3' : [0,0,1,1],
})

df1.set_index(['B','A']).stack().reset_index()

The result leaves you with:

结果给你留下:

Out[82]: 
     B   A level_2  0
0   b1  a1      D1  1
1   b1  a1      D2  0
2   b1  a1      D3  0
3   b2  a1      D1  0
4   b2  a1      D2  1

You could do:

你可以这样做:

df1.set_index(['B','A']).stack().reset_index(name='my_col')

In order to set the name of the last column but I'm wondering if there is a way to use the parameter to set the name of the 'level_2' column as well.

为了设置最后一列的名称,但我想知道是否有一种方法可以使用该参数来设置“level_2”列的名称。

The first thing that came to my mind was to try:

我想到的第一件事是尝试:

df1.set_index(['B','A']).stack().reset_index(name=['my_col2','my_col'])

However, that did not work so looking for another way around. I realize I could always just rename the columns in the next line but was hoping there'd be a cleaner way to do it in one line.

然而,这不起作用,所以寻找另一种方法。我意识到我总是可以重命名下一行中的列,但希望有一种更简洁的方法可以在一行中做到这一点。

Thanks! Sam

谢谢!山姆

采纳答案by Psidom

reset_indexis not smart enough to do this, but we could leverage methods rename_axisand renameto give names to the index and columns/series before resetting the index; once the names are set up properly, reset_indexwill automatically convert these names to the column names in the result:

reset_index不够聪明,无法做到这一点,但我们可以利用方法rename_axisrename在重置索引之前为索引和列/系列命名;一旦名称设置正确,reset_index将自动将这些名称转换为结果中的列名称:

Here rename_axisgives names to index which is somewhat equivalent to df.index.names = ...except in a functional style; renamegives name to the Series object:

这里rename_axis给出了索引的名称,这有点等同df.index.names = ...于功能风格的除外;rename为 Series 对象命名:

df1.set_index(['B','A']).stack().rename_axis(['B','A','col2']).rename('col').reset_index()

#    B   A  col2    col
#0  b1  a1    D1    1
#1  b1  a1    D2    0
#2  b1  a1    D3    0
#3  b2  a1    D1    0
#4  b2  a1    D2    1
# ..