The eval()
function is used to perform an evaluation to a string describing operations on the columns of a given DataFrame
.
DataFrame.eval(expr, inplace=False, **kwargs)[source]
expr
: This is a string expression to be evaluated.inplace
: This takes a boolean value indicating if the operation is to be performed on the original DataFrame
.**kwargs
: These are the keywords that have no effect but may be accepted for compatibility with NumPy.This function returns a DataFrame
holding the results from the evaluation.
# A code to illustrate the cummax() function in Pandas# importing the pandas libraryimport pandas as pd# creating a dataframedf = pd.DataFrame([[1,2],[3,4]],columns=list('AB'))# printing the dataframeprint(df)# performing addition of A and Bprint("Addition")print(df.eval('A + B'))# performing subtraction of A and Bprint("Subtraction")print(df.eval('A - B'))
pandas
library.df
.df
.eval()
function to evaluate a string expression (addition) for the columns of the DataFrame
. We print the result to the console.eval()
function to evaluate a string expression (subtraction) for the columns of the DataFrame
. We print the result to the console.