📖 Scenario: You work in a small store and keep track of sales data in a table. You want to quickly calculate new values based on existing data without writing long code.
🎯 Goal: Learn how to use pandas.eval() to evaluate expressions on a DataFrame for fast calculations.
📋 What You'll Learn
Create a pandas DataFrame with sales data
Create a variable to hold a calculation expression
Use pandas.eval() to calculate a new column based on the expression
Print the updated DataFrame
💡 Why This Matters
🌍 Real World
Stores and businesses often need to calculate totals, discounts, or taxes quickly from sales data tables.
💼 Career
Data analysts and scientists use pandas.eval() to write fast, readable code for data transformations and calculations.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales with these columns and values: 'product': ['apple', 'banana', 'orange'], 'price': [0.5, 0.3, 0.8], 'quantity': [10, 20, 15]
Pandas
Hint
Use pd.DataFrame() with a dictionary of lists for columns.
2
Create the expression string
Create a variable called expr and set it to the string 'price * quantity' to represent the total sales calculation.
Pandas
Hint
Set expr to the string exactly as shown.
3
Calculate total sales using pandas.eval()
Use pandas.eval() with the expression expr and the sales DataFrame to create a new column called 'total' in sales.
Pandas
Hint
Use pd.eval(expr, local_dict=sales.to_dict('series')) to evaluate the expression with the DataFrame columns.
4
Print the updated DataFrame
Print the sales DataFrame to show the new 'total' column with calculated values.
Pandas
Hint
Use print(sales) to display the DataFrame.
Practice
(1/5)
1. What is the main purpose of pandas.eval() in data analysis?
easy
A. To read CSV files into pandas DataFrames
B. To create plots and visualizations from DataFrames
C. To evaluate string expressions involving pandas objects efficiently
D. To merge two DataFrames based on a key column
Solution
Step 1: Understand the role of eval()
pandas.eval() is designed to evaluate string expressions involving pandas objects like DataFrames and Series.
Step 2: Compare with other pandas functions
Functions like plotting, reading CSV, or merging are done by other pandas methods, not eval().
Final Answer:
To evaluate string expressions involving pandas objects efficiently -> Option C
Quick Check:
eval() evaluates expressions [OK]
Hint: Remember: eval() runs expressions as strings on pandas data [OK]
Common Mistakes:
Confusing eval() with plotting functions
Thinking eval() reads files
Mixing eval() with DataFrame merging
2. Which of the following is the correct syntax to add a new column z as the sum of columns x and y using pandas.eval() on DataFrame df?
easy
A. df['z'] = pd.eval('x + y', local_dict={'x': df['x'], 'y': df['y']})
B. df['z'] = pd.eval('df.x + df.y')
C. df['z'] = pd.eval('df.x + df.y', local_dict=df)
D. df['z'] = pd.eval('df["x"] + df["y"]')
Solution
Step 1: Understand how to pass variables to eval()
When using pandas.eval(), variables like columns must be passed via local_dict as Series objects.
Step 2: Check each option's correctness
df['z'] = pd.eval('x + y', local_dict={'x': df['x'], 'y': df['y']}) correctly passes x and y as Series from df. Options A and D incorrectly reference df inside the string. df['z'] = pd.eval('df.x + df.y', local_dict=df) tries to pass df directly, which is invalid.
import pandas as pd
df = pd.DataFrame({'x': [1, 2], 'y': [3, 4]})
result = pd.eval('x + y')
print(result)
medium
A. No error, prints [4, 6]
B. SyntaxError due to missing quotes
C. TypeError because x and y are integers
D. NameError because variables x and y are not defined in eval scope
Solution
Step 1: Check variable scope in eval()
The expression uses x and y but they are not passed via local_dict or global_dict.
Step 2: Understand the error raised
Because x and y are undefined in the eval environment, a NameError occurs.
Final Answer:
NameError because variables x and y are not defined in eval scope -> Option D
Quick Check:
Undefined variables in eval cause NameError [OK]
Hint: Always pass variables to eval via local_dict [OK]
Common Mistakes:
Assuming eval sees DataFrame columns automatically
Confusing SyntaxError with NameError
Not passing variables explicitly
5. You have a DataFrame df with columns temp and humidity. You want to create a new column comfort_index defined as 0.5 * temp + 0.3 * humidity using pandas.eval(). Which code correctly does this in one line?
hard
A. df['comfort_index'] = pd.eval('0.5 * temp + 0.3 * humidity')
C. df['comfort_index'] = pd.eval('0.5 * df.temp + 0.3 * df.humidity')
D. df['comfort_index'] = 0.5 * df['temp'] + 0.3 * df['humidity']
Solution
Step 1: Understand how to use eval() with DataFrame columns
Columns must be passed as variables in local_dict for eval() to recognize them.
Step 2: Check each option
df['comfort_index'] = pd.eval('0.5 * temp + 0.3 * humidity', local_dict={'temp': df['temp'], 'humidity': df['humidity']}) correctly passes temp and humidity as Series in local_dict. Options A and B fail to pass variables, causing errors. df['comfort_index'] = 0.5 * df['temp'] + 0.3 * df['humidity'] is valid pandas arithmetic but does not use eval().
Step 3: Confirm the requirement
The question asks for a one-line solution using pandas.eval(), so df['comfort_index'] = pd.eval('0.5 * temp + 0.3 * humidity', local_dict={'temp': df['temp'], 'humidity': df['humidity']}) is correct.