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.
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()
, andreset_index()
, to modify and manipulate the index order efficiently.
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.
# Quick examples of change the order of index of a series
# 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])
Syntax of Series.reindex() Function
Following is the syntax of creating Series.reindex() function.
# Syntax of Series.reindex() function
Series.reindex(index=None, *, axis=None, method=None, copy=None, level=None, fill_value=None, limit=None, tolerance=None)
Parameters of the Pandas Series reindex()
Following are the parameters of the Series reindex() function.
index
(array-like, optional) – New labels/index to conform the Series toaxis
({0 or ‘index’}) – Axis to target (only ‘index’ is valid for Series)method
({None, ‘backfill’/’bfill’, ‘pad’/’ffill’}, optional) – Method to use for filling holes in reindexed Series:None
– don’t fill gaps'pad'/'ffill'
– propagate last valid observation forward'backfill'/'bfill'
– use next valid observation to fill gap
copy
(bool, default True) – Return a new object even if the passed indexes are the samelevel
(int or name, optional) – For MultiIndex, level to match index values onfill_value
(scalar, default np.NaN) – Value to use for missing valueslimit
(int, optional) – Maximum number of consecutive elements to forward or backward filltolerance
(optional) – Maximum distance between original and new labels for fuzzy matches
Return Value of reindex()
This function returns a series with changed order of the index.
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
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
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
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
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.
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.
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.
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.
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 !!
Related Articles
- Pandas Series astype() Function
- Pandas Series.max() Function
- Pandas Series sum() Function
- Pandas Series.mean() Function
- Pandas Series concat() Function
- Pandas Series.min() Function
- Pandas Convert Series to Json
- Convert Pandas Series to DataFrame
- Pandas Select Columns by Name or Index
- How to create Pandas Series in Python?
- How to Get the Length of a Series in Pandas?
- Pandas Series unique() Function with Examples
- How to Add an Empty Column to a Pandas DataFrame
- How to Combine Two Series into pandas DataFrame