Python 我可以为重置索引指定名称吗?

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

Can I assign a reset index a name?

pythonpandas

提问by Demetri Pananos

Normally when a dataframe undergoes a reset_index()the new column is assigned the name indexor level_idepending on the level.

通常当一个数据框经历一个reset_index()新的列被分配名称indexlevel_i取决于级别。

Is it possible to assign the new column a name?

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

回答by EdChum

You can call renameon the returned df from reset_index:

您可以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

Set the index name

设置索引名称

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

call reset_indexand chain with rename:

调用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

Thanks to @ayhan

感谢@ayhan

alternatively you can use rename_axisto rename the index prior to reset_index:

或者,您可以使用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

or just overwrite the index name directly first:

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

df.index.name = 'bar'

and then call reset_index

然后打电话 reset_index

回答by arno_v

For a Seriesyou 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

回答by igorkf

You could do this (Jan of 2020):

您可以这样做(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

回答by Esostack

If you're using reset_index() to go from a Series to a DataFrame you can name the column like this

如果您使用 reset_index() 从系列到数据帧,您可以像这样命名列

my_series.rename('Example').reset_index()