Python - Dynamically evaluate an expression from a formula in Pandas

Given a Pandas Dataframe, we have to dynamically evaluate an expression from a formula. By Pranit Sharma Last updated : September 26, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

Given a Pandas Dataframe, we have to dynamically evaluate an expression from a formula.

Evaluating an expression from a formula

To evaluate an expression from a formula in pandas, we will use pandas.eval() method. This method is used to evaluate an arithmetic expression where different types of arithmetic operators like +,-,*,/ etc. are supported.

The syntax of the pandas.eval() method:

pandas.eval(
    expr, 
    parser='pandas', 
    engine=None, 
    truediv=NoDefault.no_default, 
    local_dict=None, 
    global_dict=None, 
    resolvers=(), 
    level=0, 
    target=None, 
    inplace=False)

The following are the Parameter(s) of pandas.eval() method:

  • expr: The expression to evaluate.
  • parser='pandas': It creates a parser tree.
  • engine=None: The engine which is used to evaluate the expression.

Let us understand with the help of an example,

Python program for dynamically evaluate an expression from a formula

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dataframe

df1 = pd.DataFrame(np.random.choice(20, (10, 4)), columns=list('1234'))
df2 = pd.DataFrame(np.random.choice(20, (10, 4)), columns=list('1234'))
df3 = pd.DataFrame(np.random.choice(20, (10, 4)), columns=list('1234'))
df4 = pd.DataFrame(np.random.choice(20, (10, 4)), columns=list('1234'))

# Display Dataframe
print("DataFrame 1:\n",df1,"\n")
print("DataFrame 2:\n",df2,"\n")
print("DataFrame 3:\n",df3,"\n")
print("DataFrame 4:\n",df4,"\n")

# Using eval
x = 5
result = pd.eval("df1 == [1, 2, 3]")

# Display result
print("Result:\n",result)

Output

The output of the above program is:

DataFrame 1:
     1   2   3   4
0   6  19  16   2
1  18   3   0  12
2  17  10   7   9
3  17   4  16  11
4   7  15  19   5
5  17  19   0   9
6   7   0  17   1
7   6  15  14  11
8   0   9  10  16
9  13  17  11   9 

DataFrame 2:
     1   2   3   4
0   0   9  14  18
1   8   5   3  15
2  15  12   2  11
3   2  17  11  13
4  11   8   4   8
5   9   8   1  18
6  19   4  17  13
7   0  19   8  11
8   6  10  11  13
9   8  12   3  16 

DataFrame 3:
     1   2   3   4
0  17   9  17  18
1   8  15  13   9
2   8  19   0   3
3  16  19  19   4
4  19   6   8  12
5   9  17  16   3
6  16   0  11   5
7  11  10  17   8
8  15  13   6  11
9  18   1  12  14 

DataFrame 4:
     1   2   3   4
0  10   6  10  10
1   4  17   2  17
2  10   1  10   7
3   1   0   7   5
4   1   6   0  18
5  14  18  11   6
6   9  17   1  12
7   5   7   6  13
8  11   4  19   0
9  19   5   2   8 

Result:
        1      2      3      4
0  False  False  False   True
1  False   True  False  False
2  False  False  False  False
3  False  False  False  False
4  False  False  False  False
5  False  False  False  False
6  False  False  False   True
7  False  False  False  False
8  False  False  False  False
9  False  False  False  False

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.