Python 如何重命名熊猫系列?

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

How to rename a pandas Series?

pythonpandas

提问by tagoma

How can I change the name of a Seriesobject?

如何更改Series对象的名称?

回答by Phillip Cloud

You can do this by changing the nameattribute of your subsobject:

您可以通过更改对象的name属性来做到这一点subs

Assuming its name is 'Settle'and you want to change it to, say, 'Unsettle', just update the nameattribute, like so:

假设它的名称是,'Settle'并且您想将其更改为,例如'Unsettle',只需更新name属性,如下所示:

In [16]: s = Series(randn(10), name='Settle')

In [17]: s
Out[17]:
0    0.434
1   -0.581
2   -0.263
3   -1.384
4   -0.075
5   -0.956
6    0.166
7    0.138
8   -0.770
9   -2.146
Name: Settle, dtype: float64

In [18]: s.name
Out[18]: 'Settle'

In [19]: s.name = 'Unsettle'

In [20]: s
Out[20]:
0    0.434
1   -0.581
2   -0.263
3   -1.384
4   -0.075
5   -0.956
6    0.166
7    0.138
8   -0.770
9   -2.146
Name: Unsettle, dtype: float64

In [21]: s.name
Out[21]: 'Unsettle'

回答by tagoma

I finally renamed my Seriesobject to 'Desired_Name' as follows

我终于将我的系列对象重命名为“Desired_Name”,如下所示

# Let my_object be the pandas.Series object
my_object.name = 'Desired_Name'

Then the automatically generated name that now is read in the legend now is 'Desired_Name' against 'Settle' previously.

然后现在在图例中读取的自动生成的名称是“Desired_Name”,而不是以前的“Settle”。

回答by Kamil Sindi

s.reset_index(name="New_Name")

or

或者

s.to_frame("New_Name")["New_Name"]

回答by Biarys

Not sure why no one mentioned rename

不知道为什么没有人提到重命名

s.rename("new_name", inplace=True)