18

How can I change the name of a Series object?

cs95
  • 379,657
  • 97
  • 704
  • 746
tagoma
  • 3,896
  • 4
  • 38
  • 57
  • Now-a-days, [you can call the `rename()` function if you do not want to modify your existing Series](https://stackoverflow.com/a/55295478/4909087) (for purposes such as method chaining). – cs95 Mar 22 '19 at 08:24

4 Answers4

27

You can do this by changing the name attribute of your subs object:

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

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'
cs95
  • 379,657
  • 97
  • 704
  • 746
Phillip Cloud
  • 24,919
  • 11
  • 68
  • 88
13

Not sure why no one mentioned rename

s.rename("new_name", inplace=True)
Biarys
  • 1,065
  • 1
  • 10
  • 22
5
s.reset_index(name="New_Name")

or

s.to_frame("New_Name")["New_Name"]
Kamil Sindi
  • 21,782
  • 19
  • 96
  • 120
0

I finally renamed my Series object to 'Desired_Name' as follows

# 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.

cs95
  • 379,657
  • 97
  • 704
  • 746
tagoma
  • 3,896
  • 4
  • 38
  • 57