Pandas DataFrame多列初始化与函数调用赋值报错问题排查
Let's break down each error and fix them one by one:
1. ValueError: Must have equal len keys and value when setting with an ndarray
Root Cause
When you run df.loc[0,['u','u_hat']] = np.asarray([np.zeros(6),np.zeros(6)]), you're creating a 2x6 numpy array (shape (2,6)). Pandas interprets this as trying to assign 6 separate values to each of the 2 columns, which doesn't match your intended structure: each column in row 0 should hold a single 6-element array, not 6 individual values.
Fix
Instead of converting the list of arrays to a numpy array, pass the list directly. Pandas will correctly map each 6-element array to the corresponding column:
df.loc[0, ['u', 'u_hat']] = [np.zeros(6), np.zeros(6)]
This works because Pandas matches each element in the list to the columns specified in the loc selector (first array to u, second to u_hat).
2. Error Assigning Revenue Function Results to payoff & power Columns
Root Cause
The main issue here is ambiguity in how Pandas handles tuple assignments to multiple columns. Your revenue function returns two 6-element numpy arrays, but Pandas may misinterpret the tuple as trying to broadcast values across rows instead of assigning each array to a single cell.
Fix
Explicitly unpack the tuple returned by revenue and assign each array to its respective column. This removes any ambiguity and ensures correct assignment:
# Inside the loop payoff_arr, power_arr = revenue(df.loc[i+1,'u']) df.loc[i+1, 'payoff'] = payoff_arr df.loc[i+1, 'power'] = power_arr
You should also apply this explicit unpacking to the initial assignment:
initialoffer = [2, 5, 1 , 3, 50, 10] initial_payoff, initial_power = revenue(initialoffer) df.loc[0, 'payoff'] = initial_payoff df.loc[0, 'power'] = initial_power
Bonus: Potential Typo in Disturbance Term
I noticed in your loop you're using time[700] as the argument for np.sin:
df.loc[i+1,'u'] = np.array(df.loc[i+1,'u_hat']) + ampl*np.sin(omega*(time[700]))
This uses a fixed time value (at index 700) for all iterations. If you intended to use the current simulation step's time, change it to:
df.loc[i+1,'u'] = np.array(df.loc[i+1,'u_hat']) + ampl*np.sin(omega * time[i+1])
This ensures the disturbance varies with each step as intended.
Full Corrected Key Code Snippets
Here's the revised version of your critical code sections:
# Initial setup fixes df.loc[0, ['u', 'u_hat']] = [np.zeros(6), np.zeros(6)] initialoffer = [2, 5, 1 , 3, 50, 10] initial_payoff, initial_power = revenue(initialoffer) df.loc[0, 'payoff'] = initial_payoff df.loc[0, 'power'] = initial_power # Loop with corrected assignments for i in range(N-1): df.loc[i+1,'u_hat'] = np.array(df.loc[i,'u_hat']) + K*dt*ampl*np.sin(omega*(time[i]))*df.loc[i,'payoff'] # Use current time step for disturbance df.loc[i+1,'u'] = np.array(df.loc[i+1,'u_hat']) + ampl*np.sin(omega * time[i+1]) # Explicitly unpack revenue results payoff_arr, power_arr = revenue(df.loc[i+1,'u']) df.loc[i+1, 'payoff'] = payoff_arr df.loc[i+1, 'power'] = power_arr
These changes should resolve both errors and allow your simulation to run as expected.
内容的提问来源于stack exchange,提问作者HoOman

