Challenge - 5 Problems
Eval Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of pandas.eval() with arithmetic expression
What is the output of the following code snippet using
pandas.eval()?Pandas
import pandas as pd result = pd.eval('3 + 4 * 2') print(result)
Attempts:
2 left
💡 Hint
Remember the order of operations: multiplication before addition.
✗ Incorrect
The expression 3 + 4 * 2 is evaluated with multiplication first, so 4*2=8, then 3+8=11.
❓ data_output
intermediate2:00remaining
Evaluating a boolean expression on a DataFrame
Given the DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}), what is the output of pd.eval('df.A > 1 & df.B < 6')?Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) result = pd.eval('df.A > 1 & df.B < 6') print(result)
Attempts:
2 left
💡 Hint
Check each row: is A > 1 and B < 6 both true?
✗ Incorrect
Row 0: A=1 >1? False, B=4 <6? True → False overall.
Row 1: A=2 >1? True, B=5 <6? True → True overall.
Row 2: A=3 >1? True, B=6 <6? False → False overall.
🔧 Debug
advanced1:30remaining
Identify the error in pandas.eval() expression
What error does the following code raise?
import pandas as pd
df = pd.DataFrame({'x': [1, 2], 'y': [3, 4]})
pd.eval('df.x + df.z')Pandas
import pandas as pd df = pd.DataFrame({'x': [1, 2], 'y': [3, 4]}) pd.eval('df.x + df.z')
Attempts:
2 left
💡 Hint
Check if all columns used in the expression exist in the DataFrame.
✗ Incorrect
The column 'z' does not exist in the DataFrame, so pandas.eval() raises a KeyError.
🚀 Application
advanced2:00remaining
Using pandas.eval() to filter rows efficiently
You want to filter rows in DataFrame
df where column 'score' is greater than 50 and 'age' is less than 30. Which code using pandas.eval() produces the correct filtered DataFrame?Pandas
import pandas as pd df = pd.DataFrame({'score': [45, 60, 70], 'age': [25, 35, 28]})
Attempts:
2 left
💡 Hint
Use the 'df.' prefix for column names when not passing target=df to pd.eval(). Use bitwise '&' for element-wise logical operations.
✗ Incorrect
Option C correctly uses the 'df.' prefix to reference columns and bitwise '&' operator for element-wise AND.
Option C lacks 'df.' prefix and target=df, raising NameError: name 'score' is not defined.
Options C and D use 'and' instead of '&', which is invalid for element-wise operations on Series.
🧠 Conceptual
expert1:30remaining
Understanding the scope of variables in pandas.eval()
Consider the code below. What will be the output of
pd.eval('a + b')?
import pandas as pd
a = 5
b = 10
result = pd.eval('a + b')
print(result)Pandas
import pandas as pd a = 5 b = 10 result = pd.eval('a + b') print(result)
Attempts:
2 left
💡 Hint
pandas.eval() can access variables from the local Python environment.
✗ Incorrect
pandas.eval() evaluates the expression using variables available in the local scope, so it computes 5 + 10 = 15.