我正在尝试使用lambda函数将列值与浮点值相乘,但是会引发异常。
代码段:
m_forecasts['potential'] = m_forecasts['bnchmk_qty'].apply(lambda x: x * round((m_uio + 1) / (b_uio + 1)))
错误:
line 374, in <lambda>
m_forecasts['potential'] = m_forecasts['bnchmk_qty'].apply(lambda x: x * round((m_uio + 1) / (b_uio + 1)))
TypeError: unsupported operand type(s) for *: 'function' and 'float')
该错误的根本原因是什么?请帮忙。
注意:正在使用熊猫0.17版本
head() of bnchmk_qty column:
0 <function multiplier at 0x1135ae230>
1 <function multiplier at 0x1135ae230>
2 <function multiplier at 0x1135ae230>
3 <function multiplier at 0x1135ae230>
4 <function multiplier at 0x1135ae230>
Name: bnchmk_qty, dtype: object
答案 0 :(得分:2)
对我来说,它工作得很好,但是最好省略apply
,因为在幕后循环:
m_forecasts = pd.DataFrame({'bnchmk_qty': [4.5,7.8,43.7]})
m_uio = 10.5
b_uio = 4.8
m_forecasts['potential'] = m_forecasts['bnchmk_qty'] * round((m_uio + 1) / (b_uio + 1))
print (m_forecasts)
bnchmk_qty potential
0 4.5 9.0
1 7.8 15.6
2 43.7 87.4
我测试代码生成函数列:
df = pd.DataFrame({'quantity': {pd.Timestamp('2011-01-31 00:00:00'): 32.21, pd.Timestamp('2011-02-28 00:00:00'): 28.32, pd.Timestamp('2011-03-31 00:00:00'): 27.12, pd.Timestamp('2011-04-30 00:00:00'): 29.56, pd.Timestamp('2011-05-31 00:00:00'): 31.98, pd.Timestamp('2011-06-30 00:00:00'): 26.25, pd.Timestamp('2011-07-31 00:00:00'): 24.75, pd.Timestamp('2011-08-31 00:00:00'): 25.56, pd.Timestamp('2011-09-30 00:00:00'): 26.68, pd.Timestamp('2011-10-31 00:00:00'): 29.12, pd.Timestamp('2011-11-30 00:00:00'): 33.87, pd.Timestamp('2011-12-31 00:00:00'): 35.45}})
print (df)
quantity
2011-01-31 32.21
2011-02-28 28.32
2011-03-31 27.12
2011-04-30 29.56
2011-05-31 31.98
2011-06-30 26.25
2011-07-31 24.75
2011-08-31 25.56
2011-09-30 26.68
2011-10-31 29.12
2011-11-30 33.87
2011-12-31 35.45
import statsmodels.api as sm
p,d,q = 0,0,0
model = sm.tsa.ARIMA(endog=df['quantity'].astype(float), order=(p, d, q)).fit() ;
fsales = model.forecast(12)[0]
print (fsales)
[29.23916666 29.23916666 29.23916666 29.23916666 29.23916666 29.23916666
29.23916666 29.23916666 29.23916666 29.23916666 29.23916666 29.23916666]
df['Forecast'] = fsales
print (df)
quantity Forecast
2011-01-31 32.21 29.239167
2011-02-28 28.32 29.239167
2011-03-31 27.12 29.239167
2011-04-30 29.56 29.239167
2011-05-31 31.98 29.239167
2011-06-30 26.25 29.239167
2011-07-31 24.75 29.239167
2011-08-31 25.56 29.239167
2011-09-30 26.68 29.239167
2011-10-31 29.12 29.239167
2011-11-30 33.87 29.239167
2011-12-31 35.45 29.239167
print(sm.version.version)
'0.8.0'