Pandas 重命名索引。

6
我有以下数据集,我想将索引从summary改为id:
summary  student  count 
0        error    6
1        yes      1
2        no       1
3        other    9

我已经尝试了如下语句:newdf = df.reset_index().rename(columns={df.index.name:'foo'}) 它的作用是重置索引,并将索引列名称更改为 "foo"。
summary  index    student  count    
0        0        error   6
1        1        yes     1
2        2        no      1
3        3        other   9

我也尝试过:df.index.rename('foo', inplace = True),结果如下:

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

我也尝试过:df.rename_axis('why', inplace = True),它会产生以下结果:

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

当我执行 df.dtypes 命令时:
summary
student object
count   init64
dtype:  object

我想要的:

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

或:

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

这个问题在这里已经有答案了:https://dev59.com/6mIj5IYBdhLWcg3w04Mo - vercelli
3个回答

13

您需要移除列名:

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')
问题在于'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

当您尝试添加索引名称时,很明显'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

然而,这里并没有歧义:当您有一个索引名称时,列会提高一个级别,这使您能够区分索引名称和列名称。

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

5
你需要访问索引的属性。
df.index.name = 'id'

原始内容

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

固定df:

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

更新:看起来您为该列索引设置了名称。您应该使用以下代码将其删除: df.columns.names = ''
请注意保留HTML标记。

df.index.name = 'id' 会在 summary 的基础上添加 id,而不是将 summary 重命名为 id。不确定发生了什么。 - a1234

1

首先,您可以删除该列:

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

然后您就可以获得期望的结果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接