pandas 熊猫重命名索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55027108/
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-14 06:20:32  来源:igfitidea点击:

Pandas rename index

pythonpandas

提问by a1234

I have the following dataframe, where I want to rename the index fromsummaryto id:

我有以下数据框,我想将索引重命名summaryid

summary  student  count 
0        error    6
1        yes      1
2        no       1
3        other    9

I have tried: newdf = df.reset_index().rename(columns={df.index.name:'foo'})which gives:

我试过: newdf = df.reset_index().rename(columns={df.index.name:'foo'})它给出:

summary  index    student  count    
0        0        error   6
1        1        yes     1
2        2        no      1
3        3        other   9

I have also tried: df.index.rename('foo', inplace = True)which gives:

我也试过:df.index.rename('foo', inplace = True)它给出:

 summary     student  count
 foo        
 0           error    6
 1           yes      1
 2           no       1
 3           other    9

I have also tried: df.rename_axis('why', inplace = True)which gives:

我也试过:df.rename_axis('why', inplace = True)它给出:

 summary     student  count
 why        
 0           error    6
 1           yes      1
 2           no       1
 3           other    9

When I do df.dtypes:

当我这样做时df.dtypes

summary
student object
count   init64
dtype:  object

What I would like:

我想要什么:

id  student  count 
0   error    6
1   yes      1
2   no       1
3   other    9

OR:

或者:

    student  count 
0   error    6
1   yes      1
2   no       1
3   other    9

回答by ALollz

You need to remove the column name:

您需要删除列名:

df.rename_axis(None, axis=1).rename_axis('id', axis=0)
##if pd.__version__ == 0.24.0 
#df.rename_axis([None], axis=1).rename_axis('id')


The problem is that 'summary'is your column name. When there is no index name, the column name is placed directly above the index, which can be misleading:

问题是'summary'你的列名。当没有索引名时,列名直接放在索引的上方,这可能会产生误导:

import pandas as pd
df = pd.DataFrame([[1]*2]*4, columns=['A', 'B'])
df.columns.name = 'col_name'
print(df)

#col_name  A  B
#0         1  1
#1         1  1
#2         1  1
#3         1  1

When you then try to add an index name, it becomes clear that 'col_name'was really the column name.

当您然后尝试添加索引名称时,很明显这'col_name'确实是列名称。

df.index.name = 'idx_name'
print(df)

#col_name  A  B
#idx_name      
#0         1  1
#1         1  1
#2         1  1
#3         1  1

There is no ambiguity though: when you have an index name, the columns are raised one level, which allows you to distinguish between an index name and a column name.

但是没有歧义:当您有索引名称时,列会升高一级,这样您就可以区分索引名称和列名称。

df = pd.DataFrame([[1]*2]*4, columns=['A', 'B'])
df.index.name = 'idx_name'
print(df)

#          A  B
#idx_name      
#0         1  1
#1         1  1
#2         1  1
#3         1  1

回答by Yuca

you need to access the index's properties

您需要访问索引的属性

df.index.name = 'id'

original

原来的

         student  count
summary               
0         error      6
1           yes      1
2            no      1
3         other      9

fixed df:

固定 df:

    student  count
id               
0    error      6
1      yes      1
2       no      1
3    other      9

Update: seems like you had a name for the column's index. You should remove it with

更新:似乎您有列索引的名称。你应该删除它

df.columns.names = ''

df.columns.names = ''

回答by Mohit Musaddi

First you can drop the column:

首先,您可以删除该列:

df = df.drop('summary', axis=1)
df['id'] = np.arange(df.shape[0])
df.set_index('id', inplace=True)

Then you can get the desired result.

然后就可以得到想要的结果了。