我可以为重置索引分配名称吗?

Dem*_*nos 31 python pandas

通常,当数据帧经历reset_index()新列时,会为其分配名称indexlevel_i根据级别.

是否可以为新列指定名称?

EdC*_*ica 35

您可以renamereset_index以下位置调用返回的df :

In [145]:
# create a df
df = pd.DataFrame(np.random.randn(5,3))
df

Out[145]:
          0         1         2
0 -2.845811 -0.182439 -0.526785
1 -0.112547  0.661461  0.558452
2  0.587060 -1.232262 -0.997973
3 -1.009378 -0.062442  0.125875
4 -1.129376  3.282447 -0.403731
Run Code Online (Sandbox Code Playgroud)

设置索引名称

In [146]:    
df.index = df.index.set_names(['foo'])
df

Out[146]:
            0         1         2
foo                              
0   -2.845811 -0.182439 -0.526785
1   -0.112547  0.661461  0.558452
2    0.587060 -1.232262 -0.997973
3   -1.009378 -0.062442  0.125875
4   -1.129376  3.282447 -0.403731
Run Code Online (Sandbox Code Playgroud)

电话reset_index和连锁rename:

In [147]:
df.reset_index().rename(columns={df.index.name:'bar'})

Out[147]:
   bar         0         1         2
0    0 -2.845811 -0.182439 -0.526785
1    1 -0.112547  0.661461  0.558452
2    2  0.587060 -1.232262 -0.997973
3    3 -1.009378 -0.062442  0.125875
4    4 -1.129376  3.282447 -0.403731
Run Code Online (Sandbox Code Playgroud)

感谢@ayhan

或者你可以rename_axis用来重命名索引reset_index:

In [149]:
df.rename_axis('bar').reset_index()

Out[149]:
   bar         0         1         2
0    0 -2.845811 -0.182439 -0.526785
1    1 -0.112547  0.661461  0.558452
2    2  0.587060 -1.232262 -0.997973
3    3 -1.009378 -0.062442  0.125875
4    4 -1.129376  3.282447 -0.403731
Run Code Online (Sandbox Code Playgroud)

或者直接首先直接覆盖索引名称:

df.index.name = 'bar'
Run Code Online (Sandbox Code Playgroud)

然后打电话 reset_index

  • 在使用[rename_axis](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rename_axis.html)和重置索引更新的版本也是一种选择. (6认同)
  • @ayhan是正确的,但是我认为OP想要知道是否可以重命名`reset_index`的结果,而不是在重设之前重命名索引,尽管如此,我将进行更新和补充,谢谢 (2认同)
  • “或者你可以”之后的部分对我有用,之前的方法似乎不起作用(它不会重命名旧列,将其保留为“索引”)。 (2认同)

igo*_*rkf 26

您可以这样做(2020 年 1 月):

df = df.reset_index().rename(columns={'index': 'bar'})
print(df)
   bar         0         1         2
0    0 -2.845811 -0.182439 -0.526785
1    1 -0.112547  0.661461  0.558452
2    2  0.587060 -1.232262 -0.997973
3    3 -1.009378 -0.062442  0.125875
4    4 -1.129376  3.282447 -0.403731
Run Code Online (Sandbox Code Playgroud)


Ynj*_*jmh 9

您可以尝试1.5.0 版本names中引入的参数,来自:DataFrame.reset_index()

名称int、str 或一维列表,默认 None

使用给定的字符串,重命名包含索引数据的 DataFrame 列。如果 DataFrame 具有 MultiIndex,则它必须是长度等于级别数的列表或元组。

对于单个索引级别数据帧:

         class  max_speed                                        name   class  max_speed
falcon    bird      389.0   df.reset_index(names='name')    0  falcon    bird      389.0
parrot    bird       24.0  ------------------------------>  1  parrot    bird       24.0
lion    mammal       80.5                                   2    lion  mammal       80.5
monkey  mammal        NaN                                   3  monkey  mammal        NaN
Run Code Online (Sandbox Code Playgroud)

对于多索引级别数据帧:

               speed species                                                   classes   names  speed species
                 max    type                                                                      max    type
class  name                                                                  0    bird  falcon  389.0     fly
bird   falcon  389.0     fly   df.reset_index(names=['classes', 'names'])    1    bird  parrot   24.0     fly
       parrot   24.0     fly  -------------------------------------------->  2  mammal    lion   80.5     run
mammal lion     80.5     run                                                 3  mammal  monkey    NaN    jump
       monkey    NaN    jump
Run Code Online (Sandbox Code Playgroud)

如果您只关心 Multiindex 中的一级

               speed species                                                            classes  speed species
                 max    type                                                                       max    type
class  name                    df.reset_index(level='class', names='classes')    name
bird   falcon  389.0     fly  ------------------------------------------------>  falcon    bird  389.0     fly
       parrot   24.0     fly                                                     parrot    bird   24.0     fly
mammal lion     80.5     run                                                     lion    mammal   80.5     run
       monkey    NaN    jump                                                     monkey  mammal    NaN    jump
Run Code Online (Sandbox Code Playgroud)

注意:reset_index()默认情况下不放置,您需要将结果分配回或使用inplace=True,例如

         class  max_speed                                        name   class  max_speed
falcon    bird      389.0   df.reset_index(names='name')    0  falcon    bird      389.0
parrot    bird       24.0  ------------------------------>  1  parrot    bird       24.0
lion    mammal       80.5                                   2    lion  mammal       80.5
monkey  mammal        NaN                                   3  monkey  mammal        NaN
Run Code Online (Sandbox Code Playgroud)


arn*_*o_v 5

For a Series you can specify the name directly. E.g.:

>>> df.groupby('s1').size().reset_index(name='new_name')
  s1  new_name
0  b         1
1  r         1
2  s         1
Run Code Online (Sandbox Code Playgroud)

  • 我不知道为什么,但有时不起作用,并出现此错误:TypeError:reset_index()得到了意外的关键字参数'name' (6认同)