Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22882

Python for Beginners: Rename Index in a Pandas Series

$
0
0

We use pandas series objects for various data processing tasks in python. In this article, we will discuss how to rename the index in a pandas series.

Rename Index in a Pandas Series Using the index Attribute

When a series is created, the name of the index is empty. To rename the index of the series, you can use the name attribute of the series index object. You can assign the new index name to the name attribute of the index object to rename the series index as shown below.

import pandas as pd
import numpy as np
letters=["a","b","c","ab","abc","abcd","bc","d"]
numbers=[3,23,11,14,16,2,45,65]
series=pd.Series(letters,index=numbers)
series.index.name="Numbers"
print("The series is:")
print(series)

Output:

The series is:
Numbers
3        a
23       b
11       c
14      ab
16     abc
2     abcd
45      bc
65       d
dtype: object

In the above example, We have first created a pandas series using the Series() constructor. Then, we assigned the string "Numbers" to the index.name attribute of the pandas series. Hence, the series index is renamed to "Numbers".

To rename the index of a series, you can also use the rename_axis() method. 

Rename the Index of a Series Using the rename_axis() Method

The rename_axis() method has the following syntax.

Series.rename_axis(mapper=_NoDefault.no_default, *, inplace=False, **kwargs)

Here, 

  • The mapper parameter takes the new name of the index as its input argument. 
  • By default, the rename_axis() method returns a new series object. To modify the original series on which the rename_axis() method is invoked, you can set the inplace parameter to True.

After execution, the rename_axis() method returns a new series with renamed index as shown below.

import pandas as pd
import numpy as np
letters=["a","b","c","ab","abc","abcd","bc","d"]
numbers=[3,23,11,14,16,2,45,65]
series=pd.Series(letters,index=numbers)
series=series.rename_axis("Numbers")
print("The series is:")
print(series)

Output:

The series is:
Numbers
3        a
23       b
11       c
14      ab
16     abc
2     abcd
45      bc
65       d
dtype: object

In the above example, we first created a series. Then, we used the rename_axis() method to rename the index column of the series. Here, the rename_axis() method returns a new series instead of modifying the original series.

Suggested Reading: If you are into machine learning, you can read this MLFlow tutorial with code examples. You might also like this article on clustering mixed data types in Python.

Rename Index in a Series Inplace in Python

You can also modify the original series instead of creating a new series object after renaming the index. For this, you can set the inplace parameter to True in the rename_axis() method as shown below.

import pandas as pd
import numpy as np
letters=["a","b","c","ab","abc","abcd","bc","d"]
numbers=[3,23,11,14,16,2,45,65]
series=pd.Series(letters,index=numbers)
series.rename_axis("Numbers",inplace=True)
print("The series is:")
print(series)

Output:

The series is:
Numbers
3        a
23       b
11       c
14      ab
16     abc
2     abcd
45      bc
65       d
dtype: object

In this example, we have set the inplace parameter to True in the rename_axis() parameter. Hence, the index of the original series has been renamed instead of creating a new series.

Conclusion

In this article, we have discussed how to rename the index in a pandas series using the index attribute and the renam_axis() method. To know more about the pandas module, you can read this article on how to sort a pandas dataframe. You might also like this article on how to drop columns from a pandas dataframe.

The post Rename Index in a Pandas Series appeared first on PythonForBeginners.com.


Viewing all articles
Browse latest Browse all 22882

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>