0
0
Pandasdata~10 mins

eval() for expression evaluation in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to evaluate the expression '2 + 3' using eval().

Pandas
result = eval([1])
print(result)
Drag options to blanks, or click blank then click option'
A"2 + 3"
B2 + 3
C'2 + 3'
D2 * 3
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the expression without quotes causes a syntax error.
Using single quotes without escaping inside eval can cause errors.
2fill in blank
medium

Complete the code to evaluate the expression stored in the variable 'expr' using eval().

Pandas
expr = '10 / 2'
result = eval([1])
print(result)
Drag options to blanks, or click blank then click option'
Aexpr
B"expr"
C'expr'
D10 / 2
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the variable name as a string instead of the variable itself.
Trying to evaluate the expression without storing it in a variable.
3fill in blank
hard

Fix the error in the code to evaluate the expression 'df["A"] + df["B"]' using pandas.eval().

Pandas
import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
result = pd.eval([1])
print(result)
Drag options to blanks, or click blank then click option'
Adf.A + df.B
Bdf['A'] + df['B']
C"df['A'] + df['B']"
D'df["A"] + df["B"]'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the expression as a Python expression instead of a string.
Using inconsistent quotes causing syntax errors.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that uses eval() to filter words longer than 3 characters.

Pandas
words = ['cat', 'lion', 'dog', 'elephant']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length.
Filtering by comparing the word string to a number.
5fill in blank
hard

Fill all three blanks to create a pandas.eval() expression that calculates the sum of columns 'X' and 'Y' for rows where 'Z' > 5.

Pandas
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3], 'Y': [4, 5, 6], 'Z': [6, 4, 7]})
result = df.loc[df.eval([1]), [2] + [3]]
print(result)
Drag options to blanks, or click blank then click option'
A"Z > 5"
B'X'
C'Y'
D'Z'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the filter expression without quotes.
Using wrong column names or missing quotes.