Built with Tutify.io

    AIStudyLabs

    Pandas DataFrame eval() Function

    Pandas DataFrame eval() Function

    In pandas, the eval() function is used to evaluate string expressions using the DataFrame's columns directly. This function is useful for performing arithmetic operations, comparisons, and other computations more concisely and efficiently.

    In this article, I will explain the pandas DataFrame eval() function and using its syntax, parameters, and usage how we can apply multiple string expressions directly on the DataFframes columns. Also explained using the inplace parameter how we can update the dataFrame directly without creating a copy.

    Key Points –

    <a id="h-syntax-of-pandas-dataframe-eval-function"></a>

    Syntax of Pandas DataFrame eval() Function

    Following is the syntax of the pandas DataFrame eval() function.

    
    # Syntax of the eval() function
    DataFrame.eval(expr, inplace=False, **kwargs)
    

    <a id="h-parameters-of-the-nbsp-dataframe"></a>

    Parameters of the DataFrame

    Following are the parameters of the DataFrame eval() function.

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

    Return Value

    It returns a pandas object containing the result of the evaluation.

    <a id="h-usage-of-pandas-dataframe-eval-function"></a>

    Usage of Pandas DataFrame eval() Function

    The eval() function in pandas DataFrame is used for evaluating string expressions that involve DataFrame columns.

    To run some examples of the Pandas DataFrame eval() function, let’s create a Pandas DataFrame using data from a dictionary.

    
    # Create DataFrame
    import pandas as pd
    studentdetails = {
           "student_name":["Ram","Sam","Scott","Ann","John"],
           "mathematics" :[80,90,85,70,95],
           "science" :[85,95,80,90,75],
           "english" :[90,85,80,70,95]
                  }
    index_labels=['r1','r2','r3','r4','r5']
    df = pd.DataFrame(studentdetails ,index=index_labels)
    print("Create DataFrame:\n", df
    

    Yields below output.

    <a id="h-creating-a-new-column"></a>

    Creating a New Column

    To create a new column in a pandas DataFrame, you can use the eval() function or directly assign a new column based on calculations involving existing columns. Let's create a new column based on the existing column using the eval() function.

    
    # Create a new column using eval() function
    df.eval('total_score = mathematics + science + english', inplace=True)
    print("DataFrame with new column:\n", df)
    

    Here,

    <a id="h-updating-an-existing-column"></a>

    Updating an Existing Column

    Alternatively, to update an existing column in a pandas DataFrame, you can use the eval() function or direct assignment to change the column's values. Let's update the specified column using the eval() function with the inplace parameter to modify the DataFrame directly.

    
    # Update the 'mathematics' column by adding 5 bonus points
    df.eval('mathematics = mathematics + 5', inplace=True)
    print("DataFrame after Updating 'mathematics' Column:\n", df)
    
    # Output:
    # DataFrame after Updating 'mathematics' Column:
    #    student_name  mathematics  science  english
    # r1          Ram           85       85       90
    # r2          Sam           95       95       85
    # r3        Scott           90       80       80
    # r4          Ann           75       90       70
    # r5         John          100       75       95
    

    Here,

    <a id="h-performing-a-complex-calculation"></a>

    Performing a Complex Calculation

    To perform a complex calculation using the eval() function in pandas, you can combine multiple operations and references to different DataFrame columns in a single expression.

    Let's perform a complex calculation to create a new column based on existing columns.

    
    # Perform a complex calculation to create a 'average_score' column
    df.eval('average_score = (mathematics * 0.4 + science * 0.3 + english * 0.3)', inplace=True)
    print("DataFrame with complex calculation 'average_score' column:\n", df)
    
    # Output:
    # DataFrame with complex calculation 'average_score' column:
    #    student_name  mathematics  science  english  average_score
    # r1          Ram           80       85       90           84.5
    # r2          Sam           90       95       85           90.0
    # r3        Scott           85       80       80           82.0
    # r4          Ann           70       90       70           76.0
    # r5         John           95       75       95           89.0
    

    Here,

    <a id="h-using-local-variables-in-expressions"></a>

    Using Local Variables in Expressions

    Similarly, you can use local variables within expressions evaluated by the pandas eval() function. To include a local variable in an expression, prefix the variable name with the @ symbol. This allows you to incorporate dynamic, external values into your DataFrame computations.

    
    # Local variable to hold the highest score in the DataFrame
    max_score = df[['mathematics', 'science', 'english']].max().max()
    
    # Use eval() to calculate the normalized score using the local variable
    df.eval('normalized_score = (mathematics + science + english) / @max_score', inplace=True)
    print("DataFrame with local variable 'normalized_score' column:\n", df)
    
    # Output:
    # DataFrame with local variable 'normalized_score' column:
    #    student_name  mathematics  science  english  normalized_score
    # r1          Ram           80       85       90          2.684211
    # r2          Sam           90       95       85          2.842105
    # r3        Scott           85       80       80          2.578947
    # r4          Ann           70       90       70          2.421053
    # r5         John           95       75       95          2.789474
    

    Here,

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

    FAQ on Pandas DataFrame eval() Function

    The eval() function in pandas allows you to evaluate string expressions directly using the DataFrame's columns. This function is useful for performing arithmetic operations, comparisons, and other computations efficiently and concisely, often reducing the need for creating intermediate DataFrame objects.

    You can use local variables within expressions in the eval() function. To include a local variable, you must prefix the variable name with the @ symbol. This feature allows you to incorporate dynamic, external values into your DataFrame computations.

    eval() supports a wide range of operations, including arithmetic (addition, subtraction, multiplication, division), comparisons (greater than, less than, equal to), logical operations (and, or, not), and more complex expressions involving multiple columns.

    To modify the DataFrame in place using eval(), set the inplace parameter to True. This will apply the expression directly to the DataFrame without creating a new copy. For example: df.eval('new_column = col1 + col2', inplace=True).

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

    Conclusion

    In conclusion, the eval() function in pandas is a powerful tool for efficiently executing operations on DataFrame columns. It offers several advantages, such as reduced memory usage and enhanced performance by eliminating the need for intermediate DataFrames and optimizing expression evaluation. Using eval(), you can manage complex calculations, update existing columns, create new ones, and integrate local variables into expressions, all while keeping your code concise and readable.

    Happy Learning!!

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

    Related Articles

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

    Reference