Pandas 数据帧的操作数无法与形状错误一起广播

Operands could not be broadcast together with shapes Error for Pandas Dataframe

我查看了操作数错误的其他答案,none 似乎适合这个例子。 mathematics/equation 有效,可以在 X 值中编码或从 DataFrame 导入。 在 np.where 表达式中使用相同的等式会导致操作数错误。

import csv
import pandas as pd
from pandas import DataFrame
import numpy as np

data= pd.read_csv('miniDF.csv')
df=pd.DataFrame(data, columns=['X','Z'])
df['y']=df['Z']*0.01


df['y']=(14.6413819224756*(df['X']**0.5)+64.4092780704338*(np.log(df['X'])**-2)
                       +1675.7498523727*(np.exp(-df['X']))+3.07221083927051*np.cos(df['X']))

print(df)

df['y']=np.where(df['Z']>=(14.6413819224756*(df['X']**0.5)+64.4092780704338*(np.log(df['X'])**-2)
                      +1675.7498523727*(np.exp(-df['X']))+3.07221083927051*np.cos(df['X']),8,9))

print(df)

我的Dataframe中的值,第一个print(df)的输出和错误如下。

      X     Z           y
0   1.4     1  999.999293
1   2.0  2000  380.275104
2   3.0     3  159.114194
3   4.0     4   91.481930
4   5.0     5   69.767368
5   6.0     6   63.030212
6   7.0    70   59.591631
7   8.0     8   56.422723
8   9.0     9   54.673108
9  10.0    10   55.946732
Traceback (most recent call last):
File "/Users/willhutchins/Desktop/minitest.py", line 17, in <module>
df['y']=np.where(df['Z']>=(14.6413819224756*(df['X']**0.5)+64.4092780704338*(np.log(df['X'])**-2)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/ops/__init__.py", line 1229, in wrapper
res = na_op(values, other)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/ops/__init__.py", line 1115, in na_op
result = method(y)
ValueError: operands could not be broadcast together with shapes (10,) (3,) 

答案只是一个错位的括号,如下所示: [

更正后的代码是:

df['y']=np.where(df['Z']>=(14.6413819224756*(df['X']**0.5)+64.4092780704338*(np.log(df['X'])**-2)
                      +1675.7498523727*(np.exp(-df['X']))+3.07221083927051*np.cos(df['X'])),8,9)