Re arranging or Re ordering the row of dataframe in pandas python can be done by using reindex function. Let’s see how to
- Re arrange or re order the row of dataframe in pandas python with example
First let’s create a dataframe
import pandas as pd import numpy as np #Create a DataFrame df1 = { 'Name':['George','Andrea','micheal','maggie','Ravi', 'Xien','Jalpa'], 'Gender':["M","F","M","F","M","M","F"], 'Score':[62.7,47.7,55.6,74.6,31.5,77.3,85.4], 'Rounded_score':[63,48,56,75,32,77,85] } df1 = pd.DataFrame(df1,columns=['Name','Gender','Score','Rounded_score']) print(df1)
df1 will be
Re order the row of dataframe in pandas python
Re ordering or re arranging the row of dataframe in pandas python can be done by using reindex function and stored as new dataframe
df2= df1.reindex([2,1,0,5,6,4,3]) print(df2)
row index of the dataframe is re ordered as shown below