在python中使用列名字典决定如何通过等式进行乘法

2024-09-28 05:21:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图定义一个函数,在字典中循环,其中键是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

我哪里出错了


Tags: indfinput错误ipythonparamskeysoffset
1条回答
网友
1楼 · 发布于 2024-09-28 05:21:22

您需要逐行操作数据帧。pandas中的一个函数是apply。而不是将整个数据帧传递给offset_params,而是使用apply对整个数据帧进行迭代,逐行传递它

df['offset'] = df.apply(lambda row: offset_params(row), axis=1)

# Then look at your dataframe:

df.head()

我将把函数中的变量名df改为row,这样代码就更有意义了

相关问题 更多 >

    热门问题