Built with Tutify.io

    AIStudyLabs

    Pandas Apply Function to Every Row

    Pandas Apply Function to Every Row

    Use the apply() function when you want to update every row in the Pandas DataFrame by calling a custom function. In order to apply a function to every row, you should use the axis=1 param to the apply() function.

    By applying a function to each row, we can create a new column by using the values from the row, updating the row, etc.

    Note that by default it uses axis=0 meaning it applies a function to each column.

    Key Points –

    <a id="h-quick-examples-of-pandas-apply-function-to-every-row"></a>

    Quick Examples of Pandas Apply Function to Every Row

    Below are some quick examples of how to apply a function to every row of pandas DataFrame.

    
    # Quick examples of pandas apply function to every row
    
    # Example 1: Using Dataframe.apply() 
    # To apply function to every row
    def add(row):
       return row[0]+row[1]+row[2]
    
    df['new_col'] = df.apply(add, axis=1)
    
    # Example 2: Pandas apply function to every row 
    # Using lambda function
    df['new_col'] = df.apply(lambda row : row[0]+row[1]+row[2], axis=1)
    
    # Example 3: Add 3 to each column of a row 
    df2 = df.apply(lambda row : pd.Series([row[0]+3,row[1]+3,row[2]+3]), axis=1)
    
    # Example 4: Apply function NumPy.sum() to each row
    df['new_col'] = df.apply(np.sum, axis = 1)
    

    Let's create a sample DataFrame to work with some examples.

    
    # Create a DataFrame
    import pandas as pd
    import numpy as np
    data = [(3,5,7), (2,4,6),(5,8,9)]
    df = pd.DataFrame(data, columns = ['A','B','C'])
    print("Create DataFrame:\n", df)
    

    Yields below output.

    <a id="h-use-the-apply-function-to-every-row-of-dataframe"></a>

    Use the apply() Function to Every Row of DataFrame

    By using apply() function you can call a function to every row of pandas DataFrame. Here the add() function will be applied to every row of the Pandas DataFrame. In order to iterate row by row in the apply() function use axis=1.

    
    # Using Dataframe.apply() to apply function 
    # To every row
    def add(row):
       return row[0]+row[1]+row[2]
    
    df['new_col'] = df.apply(add, axis=1)
    print("Use the apply() function to every row:\n", df)
    

    Yields below output. This creates a new column by adding values from each column of a row.

    <a id="h-apply-lambda-to-every-row-of-dataframe"></a>

    Apply Lambda to Every Row of DataFrame

    You can use the apply() function along with a lambda function to apply a specific operation to every row of a Pandas DataFrame.

    
    # Using lambda function
    df['new_col'] = df.apply(lambda row : row[0]+row[1]+row[2], axis=1)
    print("Use the apply() function to every row:\n", df)
    

    Yields the same output as above.

    <a id="h-apply-lambda-function-to-update-each-row-all-columns"></a>

    Apply Lambda Function to Update Each Row (all columns)

    To apply a lambda function along with the apply() method to update each row in a DataFrame. The below example adds 3 to all column values for each row.

    
    # Add 3 to each column of a row 
    df2 = df.apply(lambda row : pd.Series([row[0]+3,row[1]+3,row[2]+3]), axis=1)
    print("Use the apply() function to every row:\n", df2)
    

    Yields below output.

    
    # Output:
    # Use the apply() function to every row:
       0   1   2
    0  6   8  10
    1  5   7   9
    2  8  11  12
    

    <a id="h-apply-numpy-sum-to-every-row"></a>

    Apply NumPy.sum() to Every Row

    Finally, let's see how to apply a NumPy function to each row.

    
    # Apply function NumPy.sum() to each row
    import pandas as pd
    import numpy as np
    df['new_col'] = df.apply(np.sum, axis = 1)
    print("Use the apply() function to every row:\n", df)
    

    Yields below output.

    
    # Output:
    # Use the apply() function to every row:
        A   B  C
    0   9  25  7
    1   4  16  6
    2  25  64  9
    

    <a id="h-complete-example"></a>

    Complete Example

    
    import pandas as pd
    import numpy as np
    data = [(3,5,7), (2,4,6),(5,8,9)]
    df = pd.DataFrame(data, columns = ['A','B','C'])
    print(df)
    
    # Using Dataframe.apply() 
    # To apply function to every row
    def add(row):
       return row[0]+row[1]+row[2]
    
    df['new_col'] = df.apply(add, axis=1)
    print(df)
    
    # Using lambda function
    df['new_col'] = df.apply(lambda row : row[0]+row[1]+row[2], axis=1)
    print(df)
    
    # Add 3 to each column of a row 
    df2 = df.apply(lambda row : pd.Series([row[0]+3,row[1]+3,row[2]+3]), axis=1)
    print(df2)
    
    # Apply function NumPy.sum() to each row
    df['new_col'] = df.apply(np.sum, axis = 1)
    print(df)
    

    <a id="h-frequently-asked-questions-of-pandas-apply-function-to-every-row"></a>

    Frequently Asked Questions of Pandas Apply Function to Every Row

    When applying an apply() function to every row of a DataFrame, you can set the axis parameter as 1, i.e. the function should be applied along the columns (each row). For example, df.apply(func, axis=1)

    You can pass any function that takes a single argument (a row in this case) and returns a value. This can be a built-in Python function, a lambda function, or a custom function you define.

    you can select specific columns before applying the function. For example, selected_col = [col1, col2]
    df[selected_col].apply(func, axis = 1)

    The apply function can be used on both DataFrames and Series. When used on a Series, it applies the function element-wise. For example, series.apply(func)

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

    Conclusion

    In this article, you have learned how to use the apply() function when you want to update every row in Pandas DataFrame by calling a custom function. In order to apply this function to every row, you should use the axis=1 param and use axis=0 for each column.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles

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

    References