如何使用 pandas 重命名重置索引上的多个列 [英] How to Rename Multiple Columns on a Reset Index with Pandas

查看:422
本文介绍了如何使用 pandas 重命名重置索引上的多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出在尝试重置索引时是否可以重命名Pandas列的方法.我在文档中看到,如果只有一列,则可以使用"name"参数设置重置索引的列名,但是我很好奇是否有一种方法可以对多列执行此操作.

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.

例如:

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()

结果使您拥有:

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

您可以这样做:

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

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

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.

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

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.

谢谢! 山姆

推荐答案

reset_index不够聪明,但是我们可以利用方法

reset_index is not smart enough to do this, but we could leverage methods rename_axis and rename to give names to the index and columns/series before resetting the index; once the names are set up properly, reset_index will automatically convert these names to the column names in the result:

此处rename_axis为索引赋予名称,该索引在某种程度上与df.index.names = ...等效,只是在功能上有所不同. rename为Series对象命名:

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

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
# ..

这篇关于如何使用 pandas 重命名重置索引上的多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆