python-3.x - 如何在条件列表中矢量化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()
谢谢你的帮助
解决方案
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
推荐阅读
- android - 使用毕加索禁用 recyclerview 自动滚动
- go - 卡在 Go 中的密码挑战 4
- abap - 在子查询中选择多个字段
- python - Django:如何使用 ORM 连接没有外键的列
- c# - C# 仅为此程序添加自定义 Windows 上下文菜单
- qt - QT蓝牙外设和中心角色可能吗?
- python - 在 matplotlib 中手动更改 xticks
- php - Microsoft Dynamics Nav API 错误:客户过帐组必须在客户中具有值
- docker - 如何更改 Docker 堆栈重启行为?
- computer-vision - CASIA人脸反欺骗数据库