Python Pandas Dataframe.eval() Function

Python Pandas dataframe.eval() Function:

Syntax: 

DataFrame.eval(expr, inplace=False, **kwargs)

Parameters: 

expr :

 The string of expression to evaluate.

inplace :

 If the expression involves an assignment, whether the action should be performed in situ and the existing DataFrame changed. If not, a new DataFrame is returned.

kwargs :

 For a complete list of the keyword parameters accepted by query(), see the eval() documentation.

Return Value:

ret : ndarray, scalar, or pandas object

Pandas dataframe.eval() Function in Python

Example1

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe
  • Store it in a variable.
  • Print the given dataframe
  • Add both the firstyear_marks, secondyear_marks columns of the dataframe and pass it as a new column, inplace = True as the arguments to the eval()function to perform addition operation and add it as new column to the dataframe
  • Print the dataframe after modification.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "firstyear_marks": [71, 60, 35, 85],
  "secondyear_marks": [80, 40, 25, 90]},
  
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Add both the firstyear_marks, secondyear_marks columns of the dataframe 
# and pass it as a new column, inplace = True as the arguments to the 
# eval()function to perform addition operation and add it
# as new column to the dataframe
data_frme.eval('sum = firstyear_marks +secondyear_marks', inplace = True)
  
# Print the dataframe after modification
print("The dataframe after adding 'sum' column:")
data_frme

Output:

The given Dataframe:
        firstyear_marks  secondyear_marks
virat                71                80
nick                 60                40
jessy                35                25
sindhu               85                90
firstyear_marks secondyear_marks sum
virat 71 80 151
nick 60 40 100
jessy 35 25 60
sindhu 85 90 175

Example2

Here, the given dataframe has NaN values. Over NaN values, no expression can be evaluated. As a result, the associated cells will also be NaN.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe
  • Store it in a variable.
  • Print the given dataframe
  • Add both the firstyear_marks, secondyear_marks columns of the dataframe and pass it as a new column, inplace = True as the arguments to the eval()function to perform addition operation and add it as new column to the dataframe
  • Here it returns NaN value inplace of None and do not perform addition operation on NaN values.
  • Print the dataframe after modification.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "firstyear_marks": [71, None, 35, 85],
  "secondyear_marks": [80, 40, 25, None]},
  
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Add the both the firstyear_marks, secondyear_marks columns of the dataframe 
# and pass it as a new column, inplace = True as the arguments to the 
# eval()function to perform addition operation and add it
# as new column to the dataframe
# Here it returns NaN value inplace of None and do not perform addition
# operation on NaN values.
data_frme.eval('sum = firstyear_marks +secondyear_marks', inplace = True)
  
# Print the dataframe after modification
print("The dataframe after adding 'sum' column:")
data_frme

Output:

The given Dataframe:
        firstyear_marks  secondyear_marks
virat              71.0              80.0
nick                NaN              40.0
jessy              35.0              25.0
sindhu             85.0               NaN
firstyear_marks secondyear_marks sum
virat 71.0 80.0 151.0
nick NaN 40.0 NaN
jessy 35.0 25.0 60.0
sindhu 85.0 NaN NaN

Leave a Comment