How to Rename a Pandas Series

  • Post author:
  • Post category:Pandas
  • Post last modified:October 5, 2023

Pandas Series.rename() function is used to rename/change/alter Series index labels or names for the given Series object. We can rename values from the given Pandas Series object based on the labels using rename() function. In this article, I will explain Series.rename() and use this function how to rename/change/alter Series object values by index labels.

1. Quick Examples of Series Rename

If you hurry below are quick examples of how to rename a pandas Series.


# Below are quick examples

# Example 1: Use series.rename() function
ser2 = ser.rename('Courses')

# Example 2: Use pandas.Series object
ser.name = 'Courses'

# Example 3: Use rename pandas series & inplace = true
ser.rename("Courses", inplace=True)

# Example 4: Use series.to_frame() function to rename series
ser2 =ser.to_frame("Courses")["Courses"]

# Example 5: Use series.rename() and lambda
ser2 = ser.rename(lambda x: x ** 2)

# Example 6: Use series.rename() function to changes labels
ser2 = ser.rename({1: 3, 3: 5})

# Example 7: Create the MultiIndex using series.rename() function
ser = pd.Series(['Java','Spark','PySpark','Pandas','NumPy'])
index = pd.MultiIndex.from_product([['Names'], [1000, 2000, 1500, 2500, 3000]], names =['Level 1', 'Level 2'])
ser.index = index
ser2 = ser.rename(level = 1, index = 'Fee')

2. Syntax of Pandas Series.rename()

Following is the syntax to create Series.rename() function.


# Syntax of Series.rename()
Series.rename(index=None, *, axis=None, copy=True, inplace=False, level=None, errors='ignore')

2.1 Parameters of rename()

Following are the parameters of rename().

  • index – dict-like or functions are transformations to apply to the index. Scalar or hashable sequence-like will alter the Series.name attribute.
  • axis – {0 or ‘index’}: The unused parameter needed for compatibility with DataFrame.
  • copy – bool, default True. Whether to copy underlying data.
  • inplace – This boolean parameter is default False. Whether to return a new Series. If True the value of copy is ignored.
  • level – In the case of MultiIndex, only rename labels in the specified level.

It returns a series with names or index labels altered.

3. Create Pandas Series

Pandas Series is a one-dimensional, Index-labeled data structure available only in the Pandas library. It can store all the datatypes such as strings, integers, float, and other python objects. We can access each element in the Series with the help of corresponding default indices.

Now, let’s create pandas series using a list of values.


# Create Pandas Series from list
data = ['Java', 'Spark', 'PySpark', 'Pandas', 'NumPy']
series = pd.Series(data, index=[0, 1, 2, 3, 4])
print(series)

Yields below output.


# Output:
0       Java
1      Spark
2    PySpark
3     Pandas
4      NumPy
dtype: object

4. Usage Series.rename() Function

Series.rename() function is used to rename the name object of given pandas Series. For example, Let’s create a Series.


import pandas as pd
  
# Create the Series
ser = pd.Series(['Java','Spark','PySpark','Pandas','NumPy'], index=[0,1,2,3,4])
print(ser)

Here, I have not given a name to the Series column. If you want you can add a name to a column of the Series by using the name parameter while using the Series() constructor.

Now, let’s rename the Series column name.


# Use series.rename() function
ser2 = ser.rename('Courses')
print(ser2)

# Use pandas.Series object
ser.name = 'Courses'
print(ser)

# Use rename pandas series & inplace = true
ser2 = ser.rename("Courses", inplace=True)
print(ser2)

Yields below output.


# Output:
0       Java
1      Spark
2    PySpark
3     Pandas
4      NumPy
Name: Courses, dtype: object

5. Use series.to_frame() Function to Rename Series

We can also use series.to_frame() function to rename the pandas series.


# Use series.to_frame() function to rename series
ser2 =ser.to_frame("Courses")["Courses"]
print(ser2)

Yields the same output as above.

6. Use series.rename() and Lambda

We can also use Pandas series.rename() along with the lambda function. The following example returns square index values from a series where values are changed in the labels index.


# Use series.rename() and lambda
ser2 = ser.rename(lambda x: x ** 2)
print(ser2)

Yields below output.


# Output:
0        Java
1       Spark
4     PySpark
9      Pandas
16      NumPy
dtype: object

Pandas series.rename() function also used to rename multiple labels index values or all indexes of the pandas Series. For examples.


# Use series.rename() function to changes labels
ser2 = ser.rename({1: 3, 3: 5})
print(ser2)

Yields below output.


# Output:
0       Java
3      Spark
2    PySpark
5     Pandas
4      NumPy
dtype: object

7. Using series.rename() Function to Create MultiIndex

We can also use Series.rename() function to rename the multi-index axis of the given Series object. Use this function to rename the 1st level of the series.


# Create the MultiIndex using series.rename() function
ser = pd.Series(['Java','Spark','PySpark','Pandas','NumPy'])
index = pd.MultiIndex.from_product([['Names'], [1000, 2000, 1500, 2500, 3000]], names =['Level 1', 'Level 2'])
ser.index = index
ser2 = ser.rename(level = 1, index = 'Fee')
print(ser2)

Yields below output.


# Output:
Level 1  Level 2
Names    1000          Java
         2000         Spark
         1500       PySpark
         2500        Pandas
         3000         NumPy
Name: Fee, dtype: object

8. Complete Example of Rename Pandas Series


import pandas as pd
  
# Create the Series
ser = pd.Series(['Java','Spark','PySpark','Pandas','NumPy'], index=[0,1,2,3,4])
print(ser)

# Use series.rename() function
ser2 = ser.rename('Courses')
print(ser2)

# Use pandas.Series object
ser.name = 'Courses'
print(ser)

# Use rename pandas series & inplace = true
ser2 = ser.rename("Courses", inplace=True)
print(ser2)

# Use series.to_frame() function to rename series
ser2 =ser.to_frame("Courses")["Courses"]
print(ser2)

# Use series.rename() and lambda
ser2 = ser.rename(lambda x: x ** 2)
print(ser2)

# Use series.rename() function to changes labels
ser2 = ser.rename({1: 3, 3: 5})
print(ser2)

# Create the MultiIndex using series.rename() function
ser = pd.Series(['Java','Spark','PySpark','Pandas','NumPy'])
index = pd.MultiIndex.from_product([['Names'], [1000, 2000, 1500, 2500, 3000]], names =['Level 1', 'Level 2'])
ser.index = index
ser2 = ser.rename(level = 1, index = 'Fee')
print(ser2)

9. Conclusion

In this article, I have explained how to rename a pandas series using Series.rename() function. and Also explained how to rename series with lambda function, series.to_frame(), create multi-index labels with examples.

Happy Learning !!

Related Articles

References

Malli

I am Mallikarjuna an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly documentation. Over the years, I have written hundreds of articles in Pandas, NumPy, Python, and I take pride in my ability to bridge the gap between technical experts and end-users by delivering well-structured, accessible, and informative content.

Leave a Reply

You are currently viewing How to Rename a Pandas Series