首页 > 解决方案 > 如何在条件列表中矢量化dataframe pandas daframe?

问题描述

我想用 NumPy 数组对我的数据框进行矢量化,但出现错误

这是代码:

在这里我初始化我的数据框

df2 = pd.DataFrame({'A': 1.,
                    'B': pd.Timestamp('20130102'),
                    'C': pd.Series(1, index=list(range(4)), dtype='float32'),
                    'D': [True,False,True,False],
                    'E': pd.Categorical(["test", "Draft", "test", "Draft"]),
                    'F': 'foo'})
df2

输出:

    A   B           C   D       E       F
0   1.0 2013-01-02  1.0 True    test    foo
1   1.0 2013-01-02  1.0 False   Draft   foo
2   1.0 2013-01-02  1.0 True    test    foo
3   1.0 2013-01-02  1.0 False   train   foo

在这里,我定义了应用于数据框列的函数

def IsBillingValid2(xE,yBilling):
    
    if(xE not in ['Draft','Cancelled'] and yBilling==True): #Order Edited
        return True
    
    else:      
        return False
  
  

在这里我启动我的功能

df2['BillingPostalCode_Det_StageName_Det']=IsBillingValid(df2['E'].values,df2['D'].values)

这是错误:

输出:

 ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<command-2946139111570059> in <module>
     16         return False
     17 
---> 18 df2['BillingPostalCode_Det_StageName_Det']=IsBillingValid(df2['E'].values,df2['D'].values)
     19 

<command-2041881674588848> in IsBillingValid(xStageName, yBilling)
    207 def IsBillingValid(xStageName,yBilling):
    208 
--> 209     if(xStageName not in ['Draft','Cancelled'] and yBilling==True): #Order Edited
    210         return True
    211 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

谢谢你的帮助

标签: python-3.x

解决方案


apply当您需要矢量化操作时,您尤其不需要。

使用pandas.Series.isin

df2['BillingPostalCode_Det_StageName_Det'] = ~df2["E"].isin({"Draft", "Cancelled"}) & df2["D"]
print(df2)

输出:

     A          B    C      D      E    F  BillingPostalCode_Det_StageName_Det
0  1.0 2013-01-02  1.0   True   test  foo                                 True
1  1.0 2013-01-02  1.0  False  Draft  foo                                False
2  1.0 2013-01-02  1.0   True   test  foo                                 True
3  1.0 2013-01-02  1.0  False  Draft  foo                                False

推荐阅读