Pandas DataFrame reindex() method with examples

Spread the love

The reindex() method in pandas allows you to rearrange the row and columns of a dataframe.

Examples –

Let’s create a dataframe to work with.

import pandas as pd

data = {'Apple':[89, 89, 90, 110, 125, 84],
       'Orange': [46, 46, 50, 65, 63, 48],
       'Banana': [26, 30, 30, 25, 38, 22],
       'Mango': [80, 80, 90, 125, 130, 150]}

index = ['June','May','Apr','Mar','Feb','Jan']

df = pd.DataFrame(data, index=index)
df

1 . Rearrange the rows or index with reindex() method –

If you look at the above dataframe, you can see that row or index labels are in wrong order. Let’s say we want to reindex the row labels. We can do this using the reindex() method in pandas.

# create new index
new_index = ['Jan','Feb','Mar','Apr','May','June']

# reindex the dataframe
df = df.reindex(new_index)
df

2 . Rearrange the column with reindex() method –

Now, Let’s say we want to change the order of the columns in the DataFrame. Instead of Apple, Orange, Banana and Mango, we want them in reverse order. We can do this using the reindex() method.

# reindex columns
cols = ['Mango','Banana','Orange','Apple']
df = df.reindex(columns= cols)
df

3 . Handling Missing Values –

Let’s say we want to rearrange the rows but my mistake we also added the rows for the month of July and Aug for which we do not have any data. In that case by default pandas will fill these rows with missing values NaN.

# create new index
new_index = ['Jan','Feb','Mar','Apr','May','June','July','Aug']

# reindex the dataframe
df.reindex(new_index)
df

We can fill these missing values using the fill_value parameter.

# fill missing values
df.reindex(new_index, fill_value=0)

Leave a Reply