pandas.eval()?import pandas as pd result = pd.eval('3 + 4 * 2') print(result)
The expression 3 + 4 * 2 is evaluated with multiplication first, so 4*2=8, then 3+8=11.
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}), what is the output of pd.eval('df.A > 1 & df.B < 6')?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)
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.
import pandas as pd
df = pd.DataFrame({'x': [1, 2], 'y': [3, 4]})
pd.eval('df.x + df.z')import pandas as pd df = pd.DataFrame({'x': [1, 2], 'y': [3, 4]}) pd.eval('df.x + df.z')
The column 'z' does not exist in the DataFrame, so pandas.eval() raises a KeyError.
df where column 'score' is greater than 50 and 'age' is less than 30. Which code using pandas.eval() produces the correct filtered DataFrame?import pandas as pd df = pd.DataFrame({'score': [45, 60, 70], 'age': [25, 35, 28]})
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.
pd.eval('a + b')?
import pandas as pd
a = 5
b = 10
result = pd.eval('a + b')
print(result)import pandas as pd a = 5 b = 10 result = pd.eval('a + b') print(result)
pandas.eval() evaluates the expression using variables available in the local scope, so it computes 5 + 10 = 15.
