• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:14 mins read
You are currently viewing Change the Index Order in Pandas Series

To change the index order in a Pandas Series, you can use the reindex() method. This method allows you to specify the new order of the index labels. In this article, I will explain how to rearrange the index values of a given series in a specific order. One easy way to re-arrange the index would be to reassign the same Series with the order of the indexes changed.

Advertisements

Key Points –

  • Series.reindex() in Pandas allows for changing the order of the index in a Series without altering the data.
  • It provides flexibility in rearranging the index labels according to specified criteria or a new sequence.
  • The method creates a new Series with the same data but with the index reorganized as per the specified order.
  • Index order determines the organization and access pattern of data in a Pandas Series.
  • Changing the index order allows for customizing the data presentation and analysis flow.
  • Pandas provides various methods, such as reindex(), set_index(), and reset_index(), to modify and manipulate the index order efficiently.

1. Quick Examples of Change the Order of Index of a Series

If you are in a hurry, below are some quick examples of changing the index order in the pandas series.


# Below are the quick examples.

# Example 1: Change order of index in pandas series
ser2 = ser.reindex(index = [4, 2, 5, 0, 3, 1])

# Example 2: Change index order using Series.reindex()
ser2 = ser.reindex(index = [5, 3, 0, 4, 2, 1])

2. Syntax of Series.reindex() Function

Following is the syntax of creating Series.reindex() function.


# Syntax of Series.reindex() function
Series.reindex(*args, **kwargs)

2.1 Return Value of reindex()

It returns a series with changed order of the index.

3. Create Pandas Series

Pandas Series is a one-dimensional, Index-labeled data structure available 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.

Note : Series data structure is the same as the NumPy array data structure but only one difference is that arrays indices are integers and start with 0, whereas in series, the index can be anything even strings. The labels do not need to be unique but they must be of hashable type.

Related: Sort the Series values in Pandas

Now, let’s create a series from the list.


import pandas as pd
import numpy as np
 
# Create numpy array
arr = np.array(['Java', 'Spark','PySpark', 'Pandas','NumPy','Python'])

# Create a series
ser = pd.Series(arr, index = [0, 1, 2, 3, 4, 5])
print(ser)

Yields below output.


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

4. Change Order of Index in Pandas Series

We can change/rearrange the order of index of a pandas series in any way you want by specifying the index in a list to Series.reindex(),


# Change order of index in pandas series
ser2 = ser.reindex(index = [4, 2, 5, 0, 3, 1])
print(ser2)

Here, you’re using the reindex() method to change the order of the index labels in the Series. The index is reordered according to the list [4, 2, 5, 0, 3, 1], resulting in a new Series ser2 with the reordered index. This example yields the below output.


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

5. Change Index Order With Different Order

Similarly, you can change the order of the index in a Pandas Series with a different order. For instance, the order of the index labels has been changed according to the new_index_order list [5, 3, 0, 4, 2, 1]. The reindex() method creates a new Series with the index reordered as per the provided list.


# Reindex the Series with the new order
new_index_order = [5, 3, 0, 4, 2, 1]
ser2= ser.reindex(index=new_index_order)
print(ser2)

# Change index order using Series.reindex()
ser2 = ser.reindex(index = [5, 3, 0, 4, 2, 1])
print(ser2)

Yields below output.


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

6. Complete Example


import pandas as pd
import numpy as np
 
# Create numpy array
arr = np.array(['Java', 'Spark','PySpark', 'Pandas','NumPy','Python'])

# Create a series
ser = pd.Series(arr, index = [0, 1, 2, 3, 4, 5])
print(ser)

# Example 1: Change order of index in pandas series
ser2 = ser.reindex(index = [4, 2, 5, 0, 3, 1])
print(ser2)

# Example 2: Change index order using Series.reindex()
ser2 = ser.reindex(index = [5, 3, 0, 4, 2, 1])
print(ser2)

Frequently Asked Questions on Change the Index Order in Series

How can I change the order of the index in a Pandas Series?

You can change the order of the index in a Pandas Series using the reindex() method. Specify the desired order of index labels using a list and pass it to the index parameter of the reindex() method.

How can I change the order of the index in a Pandas Series?

To change the order of the index in a Pandas Series, you can use the reindex() method. For example, the order of the index labels has been changed according to the specified new_index_order. The reindex() method creates a new Series with the index reordered as per the provided list.

What happens if the new index order contains labels not present in the original Series?

If the new index order contains labels not present in the original Series, Pandas will introduce missing values (NaN) for those labels in the resulting Series.

Can I specify the new order of the index labels manually?

You can specify the new order of the index labels manually by providing a list of index labels in the desired order. For example, the order of the index labels has been changed according to the specified new_index_order, which was manually specified as [4, 2, 0, 3, 1]. The reindex() method creates a new Series with the index reordered as per the provided list.

Is it possible to reverse the order of the index labels in a Series?

It’s possible to reverse the order of the index labels in a Pandas Series. You can achieve this using Python’s slicing notation.

Conclusion

In this article, you have learned how to change the order of index of a pandas series using Series.reindex() function with examples.

Happy Learning !!

References