Python | Pandas dataframe.eval()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
dataframe.eval() function is used to evaluate an expression in the context of the calling dataframe instance. The expression is evaluated over the columns of the dataframe.
Syntax: DataFrame.eval(expr, inplace=False, **kwargs)
expr : The expression string to evaluate.
inplace : If the expression contains an assignment, whether to perform the operation inplace and mutate the existing DataFrame. Otherwise, a new
DataFrame is returned.
kwargs : See the documentation for eval() for complete details on the keyword arguments accepted by query().
Returns: ret : ndarray, scalar, or pandas object
Example #1: Use
eval() function to evaluate the sum of all column element in the dataframe and insert the resulting column in the dataframe.
Let’s evaluate the sum over all the columns and add the resultant column to the dataframe
Example #2: Use
eval() function to evaluate the sum of any two column element in the dataframe and insert the resulting column in the dataframe. The dataframe has
Note : Any expression can not be evaluated over
NaN values. So the corresponding cells will be
Let’s evaluate the sum of column “B” and “C”.
Notice, the resulting column ‘D’ has
NaN value in the last row as the corresponding cell used in evaluation was a
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course