Reindexing in Pandas DataFrame

Reindexing in Pandas can be used to change the index of rows and columns of a DataFrame. Indexes can be used with reference to many index DataStructure associated with several pandas series or pandas DataFrame. Let’s see how can we Reindex the columns and rows in Pandas DataFrame.

Reindexing the Rows

One can reindex a single row or multiple rows by using reindex() method. Default values in the new index that are not present in the dataframe are assigned NaN.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import numpy and pandas module
import pandas as pd
import numpy as np
  
column=['a','b','c','d','e']
index=['A','B','C','D','E']
  
# create a dataframe of random values of array
df1 = pd.DataFrame(np.random.rand(5,5), 
            columns=column, index=index)
  
print(df1)
  
print('\n\nDataframe after reindexing rows: \n'
df1.reindex(['B', 'D', 'A', 'C', 'E']))

chevron_right


Output:

Example #2:



filter_none

edit
close

play_arrow

link
brightness_4
code

# import numpy and pandas module
import pandas as pd
import numpy as np
  
column = ['a', 'b', 'c', 'd', 'e']
index = ['A', 'B', 'C', 'D', 'E']
   
# create a dataframe of random values of array 
df1 = pd.DataFrame(np.random.rand(5, 5), 
        columns = column, index = index)
  
# create the new index for rows
new_index =['U', 'A', 'B', 'C', 'Z']
  
print(df1.reindex(new_index))

chevron_right


Output:

Reindexing the columns using axis keyword

One can reindex a single column or multiple columns by using reindex() method and by specifying the axis we want to reindex. Default values in the new index that are not present in the dataframe are assigned NaN.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import numpy and pandas module
import pandas as pd
import numpy as np
  
column=['a','b','c','d','e']
index=['A','B','C','D','E']
  
#create a dataframe of random values of array
df1 = pd.DataFrame(np.random.rand(5,5), 
           columns=column, index=index)
  
colum=['e','a','b','c','d']
   
# create the new index for columns
print(df1.reindex(colum, axis='columns'))

chevron_right


Output:

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import numpy and pandas module
import pandas as pd
import numpy as np
  
column =['a', 'b', 'c', 'd', 'e']
index =['A', 'B', 'C', 'D', 'E']
   
# create a dataframe of random values of array
df1 = pd.DataFrame(np.random.rand(5, 5), 
        columns = column, index = index)
  
colum =['a', 'b', 'c', 'g', 'h']
  
# create the new index for columns
print(df1.reindex(colum, axis ='columns'))

chevron_right


Output:

Replacing the missing values

Code #1: Missing values from the dataframe can be filled by passing a value to the keyword fill_value. This keyword replaces the NaN values.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import numpy and pandas module
import pandas as pd
import numpy as np
  
column =['a', 'b', 'c', 'd', 'e']
index =['A', 'B', 'C', 'D', 'E']
   
# create a dataframe of random values of array
df1 = pd.DataFrame(np.random.rand(5, 5), 
        columns = column, index = index)
  
colum =['a', 'b', 'c', 'g', 'h']
  
# create the new index for columns 
print(df1.reindex(colum, axis ='columns', fill_value = 1.5))

chevron_right


Output:

Code #2: Replacing the missing data with a string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import numpy and pandas module
import pandas as pd
import numpy as np
  
column =['a', 'b', 'c', 'd', 'e']
index =['A', 'B', 'C', 'D', 'E']
   
# create a dataframe of random values of array
df1 = pd.DataFrame(np.random.rand(5, 5), 
       columns = column, index = index)
  
colum =['a', 'b', 'c', 'g', 'h']
  
# create the new index for columns
print(df1.reindex(colum, axis ='columns', fill_value ='data missing'))

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.