Bird
Raised Fist0
Pandasdata~20 mins

eval() for expression evaluation in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Eval Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1: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)
ASyntaxError
B14
C10
D11
Attempts:
2 left
💡 Hint
Remember the order of operations: multiplication before addition.
❓ data_output
intermediate
2: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)
A[False, True, False]
B[False, True, True]
C[True, False, False]
DSyntaxError
Attempts:
2 left
💡 Hint
Check each row: is A > 1 and B < 6 both true?
🔧 Debug
advanced
1: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')
AKeyError: 'z'
BNameError: name 'df' is not defined
CSyntaxError
DTypeError
Attempts:
2 left
💡 Hint
Check if all columns used in the expression exist in the DataFrame.
🚀 Application
advanced
2: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]})
Afiltered = df[pd.eval('score > 50 and age < 30')]
Bfiltered = df[pd.eval('score > 50 & age < 30')]
Cfiltered = df[pd.eval('df.score > 50 & df.age < 30')]
Dfiltered = df[pd.eval('df.score > 50 and df.age < 30')]
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.
🧠 Conceptual
expert
1: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)
ANameError: name 'a' is not defined
B15
CTypeError
DSyntaxError
Attempts:
2 left
💡 Hint
pandas.eval() can access variables from the local Python environment.

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

  1. Step 1: Understand the role of eval()

    pandas.eval() is designed to evaluate string expressions involving pandas objects like DataFrames and Series.
  2. Step 2: Compare with other pandas functions

    Functions like plotting, reading CSV, or merging are done by other pandas methods, not eval().
  3. Final Answer:

    To evaluate string expressions involving pandas objects efficiently -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    df['z'] = pd.eval('x + y', local_dict={'x': df['x'], 'y': df['y']}) -> Option A
  4. Quick Check:

    Pass columns as local_dict to eval() [OK]
Hint: Use local_dict to pass DataFrame columns to eval() [OK]
Common Mistakes:
  • Referencing DataFrame inside eval string directly
  • Not passing columns via local_dict
  • Using wrong quotes inside expression string
3. Given the DataFrame df below:
   a  b
0  2  5
1  3  6
2  4  7

What is the output of the code?
import pandas as pd
result = pd.eval('df.a * df.b', local_dict={'df': df})
print(result.tolist())
medium
A. [10, 18, 28]
B. [7, 9, 11]
C. [2, 3, 4]
D. SyntaxError

Solution

  1. Step 1: Understand the expression df.a * df.b

    This multiplies column a by column b element-wise: [2*5, 3*6, 4*7] = [10, 18, 28].
  2. Step 2: Confirm usage of local_dict

    The DataFrame df is passed correctly in local_dict, so the expression evaluates without error.
  3. Final Answer:

    [10, 18, 28] -> Option A
  4. Quick Check:

    Element-wise multiply columns = [10, 18, 28] [OK]
Hint: Multiply columns element-wise inside eval string [OK]
Common Mistakes:
  • Forgetting to pass df in local_dict
  • Expecting sum instead of product
  • Syntax errors from missing quotes
4. What is the error in this code snippet?
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

  1. Step 1: Check variable scope in eval()

    The expression uses x and y but they are not passed via local_dict or global_dict.
  2. Step 2: Understand the error raised

    Because x and y are undefined in the eval environment, a NameError occurs.
  3. Final Answer:

    NameError because variables x and y are not defined in eval scope -> Option D
  4. 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')
B. df['comfort_index'] = pd.eval('0.5 * temp + 0.3 * humidity', local_dict={'temp': df['temp'], 'humidity': df['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

  1. Step 1: Understand how to use eval() with DataFrame columns

    Columns must be passed as variables in local_dict for eval() to recognize them.
  2. 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().
  3. 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.
  4. Final Answer:

    df['comfort_index'] = pd.eval('0.5 * temp + 0.3 * humidity', local_dict={'temp': df['temp'], 'humidity': df['humidity']}) -> Option B
  5. Quick Check:

    Pass columns via local_dict for eval() [OK]
Hint: Pass columns as local_dict to eval for new column [OK]
Common Mistakes:
  • Using df.column inside eval string without local_dict
  • Not passing variables causes NameError
  • Confusing direct arithmetic with eval usage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard