This program calculates total sales by multiplying price and quantity using eval(). Then it filters rows where total sales are more than 400.
Pandas
import pandas as pd
# Create a simple DataFrame
sales = pd.DataFrame({
'price': [100, 200, 300],
'quantity': [1, 3, 2]
})
# Calculate total sales using eval
sales['total'] = pd.eval('sales.price * sales.quantity')
# Filter rows where total sales are greater than 400
high_sales = sales[pd.eval('sales.total > 400')]
print(sales)
print('\nRows with total sales > 400:')
print(high_sales)
OutputSuccess
Important Notes
eval() can be faster than normal pandas operations for big data.
Be careful with complex expressions; test them step-by-step.
Summary
eval() lets you write math or logic expressions as strings to work with pandas data.
It helps add columns, filter data, or update values easily.
Use it to make your code shorter and sometimes faster.
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.