Google News
logo
Pandas - Interview Questions
Explain reindexing in Pandas.
Reindexing changes the row labels and column labels of a DataFrame. To reindex means to conform the data to match a given set of labels along a particular axis.
 
Multiple operations can be accomplished through indexing like :
 
* Reorder the existing data to match a new set of labels.
* Insert missing value (NA) markers in label locations where no data for the label existed.
 
Example :

import pandas as pd
import numpy as np

N=20

df = pd.DataFrame({
   'A': pd.date_range(start='2021-01-18',periods=N,freq='D'),
   'x': np.linspace(0,stop=N-1,num=N),
   'y': np.random.rand(N),
   'C': np.random.choice(['Low','Medium','High'],N).tolist(),
   'D': np.random.normal(100, 10, size=(N)).tolist()
})

#reindex the DataFrame
df_reindexed = df.reindex(index=[0,2,5], columns=['A', 'C', 'B'])

print (df_reindexed)​

 

Output :

           A              C              B

0 2021-01-18     Medium     NaN

2 2021-01-20     High          NaN

5 2021-01-23     Low           NaN

 

Advertisement