15

How can I change the name of a Series object?

cs95
  • 330,695
  • 80
  • 606
  • 657
tagoma
  • 3,637
  • 4
  • 36
  • 55
  • 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

26

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
  • 330,695
  • 80
  • 606
  • 657
Phillip Cloud
  • 23,488
  • 11
  • 67
  • 88
9

Not sure why no one mentioned rename

s.rename("new_name", inplace=True)
Biarys
  • 875
  • 1
  • 9
  • 18
5
s.reset_index(name="New_Name")

or

s.to_frame("New_Name")["New_Name"]
Kamil Sindi
  • 19,100
  • 18
  • 89
  • 115
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
  • 330,695
  • 80
  • 606
  • 657
tagoma
  • 3,637
  • 4
  • 36
  • 55