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

Python for Beginners: Create Index in a Pandas Series

$
0
0

Pandas series objects are used to store data when we need to access it using its position as well as labels. In this article, we will discuss different ways to create index in a pandas series. 

Create Index in a Pandas Series Using the Index Parameter

When we create a pandas series, it has a default index starting from 0 to the length of the series-1. For instance, consider the following example.

import pandas as pd
import numpy as np
letters=["a","b","c","ab","abc","abcd","bc","d"]
series=pd.Series(letters)
print("The series is:")
print(series)

Output:

The series is:
0       a
1       b
2       c
3      ab
4     abc
5    abcd
6      bc
7       d
dtype: object

In the above example, we have created a series of 8 elements. You can observe that the indices of the elements in the series are numbered from 0 to 7. These are the default indices.

If you want to assign a custom index to the series, you can use the index parameter in the Series() constructor. The index parameter in the Series() constructor takes a list having an equal number of elements as the elements in the series and creates a custom index for the series 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)
print("The series is:")
print(series)

Output:

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

In the above example, we have passed the python list [3, 23, 11, 14, 16, 2, 45, 65] to the index parameter of the Series() constructor. After the execution of the Series() constructor, the elements of this list are assigned as the indices of the elements in the series.

Create Index in a Pandas Series Using the Index Attribute

You can also create a new index for a series after creating the series. For instance, if you want to assign other values as indices in the series, you can use the index attribute of the series object. To create a new custom index, you can assign a list of values to the index attribute 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)
series.index=numbers
print("The series is:")
print(series)

Output:

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

In this example, we have assigned the list [3, 23, 11, 14, 16, 2, 45, 65] to the index attribute of the series after creating the series. Hence, the elements of this list are assigned as the indices of the elements in the series.

Here, the list passed to the index attribute must have a length equal to the number of elements in the series. Otherwise, the program will run into a ValueError exception. You can observe this in the following example.

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,117]
series=pd.Series(letters)
series.index=numbers
print("The series is:")
print(series)

Output:

ValueError: Length mismatch: Expected axis has 8 elements, new values have 9 elements

In the above example, you can observe that the list "letters"has only 8 elements. As a result, the series contains only 8 elements. On the other hand, the list "numbers"has 9 elements. Hence, when we assign the "numbers"list to the index attribute of the series, the program runs into a ValueError exception.

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.

Create an Index in a Pandas Series Using the set_axis() Method

Instead of using the index attribute, we can use the set_axis() method to create an index in a pandas series. 

The set_axis() Method

The set_axis() method has the following syntax.

Series.set_axis(labels, *, axis=0, inplace=_NoDefault.no_default, copy=_NoDefault.no_default)

Here, 

  • The labels parameter takes a list-like object containing index values. You can also pass an Index object to the labels parameter. The number of elements in any object passed to the labels parameter should have the same number of elements as the series on which the set_axis() method is invoked.
  • The axis parameter is used to decide if we want to create the index for rows or columns. As a Series has only one column, the axis parameter is unused. 
  • After creating a new index, the set_axis() method returns a new Series object. If you want to modify the original Series object, you can set the inplace parameter to True. 
  • The copy parameter is used to decide whether to make a copy of the underlying data instead of modifying the original series. By default, it is True. 

To create an index using the set_axis() method, we will invoke this method on the original series object. We will pass a list containing the new index values to the set_axis() method as an input argument. After execution, the set_axis() method will return a new series having a modified index. You can observe this in the following example.

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)
series=series.set_axis(labels=numbers)
print("The series is:")
print(series)

Output:

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

In this example, we have first created a series containing 8 elements. Then, we used the set_index() method to assign new indices to the elements in the series. You can observe that the set_index() method returns a new series. Hence, the original series isn’t modified. To modify the original series by assigning new indices instead of creating a new one, you can create an index in place in the series.

Create Index Inplace in a Pandas Series

To create an index inplace in a pandas series, you can assign the new index to the index attribute of the series object as shown in the following example.

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)
series.index=numbers
print("The series is:")
print(series)

Output:

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

You can also use the set_axis() method to create an index inplace in a series. For this, you can pass the list containing the new index values to the set_axis() method and set the inplace parameter to True while invoking the set_axis() method on the original series object. After execution, you will get the modified series object 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)
series.set_axis(labels=numbers,inplace=True)
print("The series is:")
print(series)

Output:

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

In this example, we used the set_index() method to assign new indices to the elements in the series. You can observe that we have set the inplace parameter to True in the set_index() method. Hence, the new indices are assigned in the original series object itself.

While using the inplace parameter you will get a FutureWarning stating "FutureWarning: Series.set_axis 'inplace' keyword is deprecated and will be removed in a future version. Use obj = obj.set_axis(..., copy=False) instead". It means that the inplace parameter has been deprecated. Hence, if the same code is used in future versions of pandas, the program may run into an error. To avoid this, you can use the copy parameter.

By default, the copy parameter is set to True. Hence, the set_axis() method uses a copy of the original series and modifies it. If you want to modify the original series, you can set the copy parameter to False in the set_axis() method.

Conclusion

In this article, we discussed different ways to create the index in a pandas series in Python. 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 Create Index in a Pandas Series appeared first on PythonForBeginners.com.


Viewing all articles
Browse latest Browse all 22891

Trending Articles