Built with Tutify.io

    AIStudyLabs

    Pandas DataFrame reindex() Function

    Pandas DataFrame reindex() Function

    Pandas DataFrame.reindex() function is used to change the row indexes and the column labels. Pandas reindex conforms DataFrame to a new index with optional filling logic and to Place NA/NaN in locations having no value in the previous index.

    Related: Pandas Reset Index from starting zero (0)

    This function takes several parameters like labels, index, columns, axis, method, copy, level, fill_value, limit, and tolerance and returns a DataFrame with a changed index/reindexed.

    In this article, I will explain the syntax, usage, and explanation with examples of how to use reindex() with single and multiple rows or columns of the DataFrame.

    Key Points –

    <a id="h-quick-examples-of-dataframe-reindex-function"></a>

    Quick Examples of DataFrame reindex() Function

    If you are in a hurry, below are some quick examples of how to use reindex() function in DataFrame.

    
    # Quick examples of DataFrame reindex() function
    
    # Example 1: Use reindex() function 
    # To reindex pandas dataframe
    df2 = df.reindex(['r1', 'index', 'r3','r4'])
    
    # Example 2: Filling the missing values with Java
    df2 = df.reindex(['r1', 'index', 'r3','r4'], fill_value = "Java")
    
    # Example 3: Use reindex() function to reindex column axis
    df2 = df.reindex(columns =["Courses", "Fee", "Duration", "Percentage"])
    
    # Example 4: Use reindex the dataframe
    df2 = df.reindex(index=['r1', 'r2','r3','r4'], columns=["Courses", "Fee", "Duration", "Percentage"])
    
    # Example 5: Reindex the columns and fill in the missing values
    df2 = df.reindex(columns =["Courses", "Fee", "Duration", "Percentage"], fill_value = "30%")
    
    # Example 6: Change reindex() function in pandas dataframe
    df2 = df.reindex(['r3', 'r4','r2','r1'])
    

    <a id="h-syntax-of-dataframe-reindex"></a>

    Syntax of DataFrame.reindex()

    Following is the syntax of DataFrame.reindex() function.

    
    # Syntax of Pandas DataFrame.reindex()
    DataFrame.reindex(labels=None, index=None, columns=None, axis=None, method=None, copy=None, level=None, fill_value=nan, limit=None, tolerance=None)
    

    <a id="h-parameters-of-reindex"></a>

    Parameters of reindex()

    Following are the parameters of pandas reindex() function.

    <a id="h-return-value-of-reindex"></a>

    Return value of reindex()

    It returns a DataFrame with a changed index/reindexed.

    Now, Let’s create Pandas DataFrame using data from a Python dictionary, where the columns are Courses, Fee, Duration and Discount.

    
    # Return value of reindex()  
    import pandas as pd
    import numpy as np
    technologies= ({
        'Courses':["Spark","PySpark","Hadoop","Pandas"],
        'Fee' :['22000','25000','24000','26000'],
        'Duration':['30days','50days','40days','60days'],
        'Discount':['1000','2300','2500','1400']
                  })
    df = pd.DataFrame(technologies, index = ['r1', 'r2', 'r3', 'r4'])
    print(df)
    

    Yields below output.

    
    # Output:
        Courses    Fee Duration Discount
    r1    Spark  22000   30days     1000
    r2  PySpark  25000   50days     2300
    r3   Hadoop  24000   40days     2500
    r4   Pandas  26000   60days     1400
    

    <a id="h-pandas-reindex-dataframe-rows"></a>

    Pandas Reindex DataFrame Rows

    We can use Pandas reindex() function to reindex the DataFrame. When we reindex a row using reindex() function, it default assigns NaN values for the new index row. Missing values can fill by passing a value to the keyword fill_value.

    
    # Use reindex() function to reindex pandas dataframe
    df2 = df.reindex(['r1', 'index', 'r3','r4'])
    print(df2)
    

    Yields below output.

    
    # Output:
           Courses    Fee Duration Discount
    r1       Spark  22000   30days     1000
    index     NaN    NaN      NaN      NaN
    r3      Hadoop  24000   40days     2500
    r4      Pandas  26000   60days     1400
    

    <a id="h-fill-missing-values-with-reindex"></a>

    Fill Missing Values with reindex()

    Notice, we have NaN values present in the new rows after reindexing, we can use the argument fill_value to the function to fill values instead on NaN. We can fill the NaN/null values using the parameter fill_value="Java" in the DataFrame.reindex() function.

    
    # Filling the missing values with Java
    df2 = df.reindex(['r1', 'index', 'r3','r4'], fill_value = "Java")
    print(df2)
    

    Yields below output.

    
    # Output:
          Courses    Fee Duration Discount
    r1      Spark  22000   30days     1000
    index    Java   Java     Java     Java
    r3     Hadoop  24000   40days     2500
    r4     Pandas  26000   60days     1400
    

    <a id="h-pandas-reindex-dataframe-columns"></a>

    Pandas Reindex DataFrame Columns

    We can also reindex the column of the Pandas DataFrame using the DataFrame.reindex() function. The specified column index, which is not in the original DataFrame will be filled by the NaN values automatically. For example.

    
    # Use reindex() function to reindex column axis
    df2 = df.reindex(columns =["Courses", "Fee", "Duration", "Percentage"])
    print(df2)
    
    # Use reindex the DataFrame
    df2 = df.reindex(index=['r1', 'r2','r3','r4'], columns=["Courses", "Fee", "Duration", "Percentage"])
    print(df2)
    

    Yields below output.

    
    # Output:
        Courses    Fee Duration  Percentage
    r1    Spark  22000   30days         NaN
    r2  PySpark  25000   50days         NaN
    r3   Hadoop  24000   40days         NaN
    r4   Pandas  26000   60days         NaN
    

    We can fill the NaN/null values using the parameter fill_value=30% in the DataFrame.reindex() function. After changing the column name, if there is a NaN value, the NaN values will be filled by the value 30%.

    
    # Reindex the columns and fill in the missing values
    df2 = df.reindex(columns =["Courses", "Fee", "Duration", "Percentage"], fill_value = "30%")
    print(df2)
    

    Yields below output.

    
    # Output:
        Courses    Fee Duration Percentage
    r1    Spark  22000   30days        30%
    r2  PySpark  25000   50days        30%
    r3   Hadoop  24000   40days        30%
    r4   Pandas  26000   60days        30%
    

    <a id="h-chang-the-rows-order-using-reindex"></a>

    Chang the Rows Order using reindex()

    We can use reindex() function to change the order of the Pandas DataFrame rows. For that we need to pass the specified list of indexes into this function, it will return DataFrame with the specified index.

    
    # Change reindex() function in pandas dataframe
    df2 = df.reindex(['r3', 'r4','r2','r1'])
    print(df2)
    

    Yields below output.

    
    # Output:
        Courses    Fee Duration Discount
    r3   Hadoop  24000   40days     2500
    r4   Pandas  26000   60days     1400
    r2  PySpark  25000   50days     2300
    r1    Spark  22000   30days     1000
    

    <a id="h-complete-example-of-reindex-function"></a>

    Complete Example of reindex() Function

    
    import pandas as pd
    import numpy as np
    technologies= ({
        'Courses':["Spark","PySpark","Hadoop","Pandas"],
        'Fee' :['22000','25000','24000','26000'],
        'Duration':['30days','50days','40days','60days'],
        'Discount':['1000','2300','2500','1400']
                  })
    df = pd.DataFrame(technologies, index = ['r1', 'r2', 'r3', 'r4'])
    print(df)
    
    # Use reindex() function to reindex pandas dataframe
    df2 = df.reindex(['r1', 'index', 'r3','r4'])
    print(df2)
    
    # Filling the missing values with Java
    df2 = df.reindex(['r1', 'index', 'r3','r4'], fill_value = "Java")
    print(df2)
    
    # Use reindex() function to reindex column axis
    df2 = df.reindex(columns =["Courses", "Fee", "Duration", "Percentage"])
    print(df2)
    
    # Use reindex the DataFrame
    df2 = df.reindex(index=['r1', 'r2','r3','r4'], columns=["Courses", "Fee", "Duration", "Percentage"])
    print(df2)
    
    # Reindex the columns and fill in the missing values
    df2 = df.reindex(columns =["Courses", "Fee", "Duration", "Percentage"], fill_value = "30%")
    print(df2)
    
    # Change reindex() function in pandas dataframe
    df2 = df.reindex(['r3', 'r4','r2','r1'])
    print(df2)
    

    <a id="h-faq-on-pandas-dataframe-reindex-function"></a>

    FAQ on Pandas DataFrame reindex() Function

    The reindex() function is used to change the index or columns of a DataFrame to match a specified set of labels, adding missing labels or removing existing ones as needed.

    To reindex rows in a Pandas DataFrame, use the reindex() method and specify the desired sequence of row labels.

    You can reindex both rows and columns simultaneously in a Pandas DataFrame by specifying values for the index and columns parameters in the reindex() method.

    Use the reindex() method with the fill_value parameter to specify a value (other than NaN) to be used for missing labels.

    reindex() changes or adjusts the existing index/columns to match a specified sequence, while set_index() replaces the index with one or more columns from the DataFrame.

    The reindex_like() method reindexes a DataFrame to match the structure (index and columns) of another DataFrame, simplifying the process when copying structures.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained how to change reindex of a Pandas DataFrame using DataFrame.reindex() function with examples. Pandas reindex conforms DataFrame to a new index with optional filling logic and to Place NA/NaN in locations having no value in the previous index.

    Happy Learning !!

    Related Articles

    References