Python - Dynamically evaluate an expression from a formula in Pandas

Given a Pandas Dataframe, we have to dynamically evaluate an expression from a formula.
Submitted by Pranit Sharma, on August 03, 2022

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.

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.

Syntax:

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

Parameter(s):

  • 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 code to 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:

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 »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.