You want to perform dynamic operations on DataFrames using pd.eval, including variable substitution and complex arithmetic.
1. Using pd.eval()
# Import necessary libraries import pandas as pd import numpy as np # Create sample DataFrames np.random.seed(0) df1 = pd.DataFrame(np.random.choice(10, (5, 4)), columns=list('ABCD')) df2 = pd.DataFrame(np.random.choice(10, (5, 4)), columns=list('ABCD')) # Evaluate expression using a variable x = 5 result = pd.eval("df1.A + (df1.B * x)") # Alternatively, assign the result to a new column pd.eval("df2['D'] = df1.A + (df1.B * x)")
The following arguments can be used to optimize pd.eval performance:
You can assign the result of pd.eval directly to a DataFrame using the target argument.
df3 = pd.DataFrame(columns=list('FBGH'), index=df1.index) pd.eval("df3['B'] = df1.A + df2.A", target=df3) # In-place modification pd.eval("df2.B = df1.A + df2.A", target=df2, inplace=True)
# Evaluate expression in df1 result = df1.eval("A + B") # Perform variable substitution df1.eval("A > @x", local_dict={'x': 5})
While pd.eval is suitable for evaluating expressions, df.query() is more concise and efficient for conditional queries, as it filters the DataFrame based on a Boolean expression.
# Query df1 df1.query("A > B")
以上是如何動態評估 Pandas 中的表達式?的詳細內容。更多資訊請關注PHP中文網其他相關文章!