Series.reindex() – Change the Index Order in Pandas Series

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 17, 2023

You can use Series.reindex() to change the order of the index values of a series in pandas. 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.

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(), for example, ser.reindex(index = [4, 2, 5, 0, 3, 1]).


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

Yields below output.


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

5. Change Index Order With Different Order

Let’s use the same example and chang the order in a different way.


# 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 For Change Order of Index in Series


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)

7. 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

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 Series.reindex() – Change the Index Order in Pandas Series