我试图定义一个函数,在字典中循环,其中键是dataframes列名,如果观察符合折扣条件,则值是与该列关联的折扣。我想将折扣相乘,并在GLM中使用产品作为补偿。我当前遇到以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-242-0029e2a0de4f> in <module>
----> 1 offset_params(df)
<ipython-input-241-b5343cde7a7e> in offset_params(df)
8
9 for keys, vals in discounts.items():
---> 10 if df[keys] == 0:
11 offset = offset * 1
12 else:
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1574 raise ValueError("The truth value of a {0} is ambiguous. "
1575 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1576 .format(self.__class__.__name__))
1577
1578 __bool__ = __nonzero__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
下面是一些产生相同错误的示例代码:
import pandas as pd
discounts = {'discount1_0':[0,1], 'discount1_1':[1,0], 'discount2_0':[0,1],'discount2_1':[1,0]
, 'discount3_0':[0,1],'discount3_1':[1,0],'discount1_0':[1,0], 'discount1_1':[0,1]
, 'discount2_0':[1,0],'discount2_1':[0,1], 'discount3_0':[1,0],'discount3_1':[0,1]
}
df = pd.DataFrame(discounts)
def offset_params(df):
discounts = {'discount1_0':1, 'discount1_1':.98, 'discount2_0':1,'discount2_1':.95
, 'discount3_0':1,'discount3_1':.95,'discount1_0':1, 'discount1_1':.98
, 'discount2_0':1,'discount2_1':.95, 'discount3_0':1,'discount3_1':.95
}
offset = 1
for keys, vals in discounts.items():
if df[keys] == 0:
offset = offset * 1
else:
offset = offset * vals
return offset
我哪里出错了
您需要逐行操作数据帧。pandas中的一个函数是
apply
。而不是将整个数据帧传递给offset_params
,而是使用apply
对整个数据帧进行迭代,逐行传递它我将把函数中的变量名
df
改为row
,这样代码就更有意义了相关问题 更多 >
编程相关推荐