Pandas – Reindexing


Rishi Sapra

Technical Community leader, speaker, trainer and evangelist specialising in Power BI and Azure. Formally recognised by Microsoft as a Most Valuable Professional (MVP), Fast Track Recognised Solution Architect (FTRSA) and Microsoft Certified Trainer (MCT).


Pandas - Reindexing

Pandas Dataframes are indexed. However, we can reindex a Dataframe, which means to create a new
object with the data conformed to a new index. This is demonstrated in tool below.
We first create an dataframe call my_frame.
Then we re-index this using the “reindex”-function and create a new dataframe.

import pandas as pd
my_frame = pd.Series([6, 7.2, -3, 400], index=['d', 'b', 'a', 'c'])
print('my_frame' + ' unindexed')

# new dataframe re-indexed
indexed = my_frame.reindex(['a', 'b', 'c', 'd'])
print ('my frame indexed')
print (indexed)

Leave a comment