【发布时间】:2018-06-09 13:51:51
【问题描述】:
我正在为以下代码行编写:
holiday['real_or_not'] = np.where((holiday['transferred']=='False',1,0))
holiday
最小可重现示例:
date type locale locale_name description transferred
2012-03-02 False locale Manta Fundacion de Manta False
2012-03-02 False Regional Regional Gunanta True
我得到:
ValueError: The truth value of a Series is ambiguous. Use a.empty,a.bool(), a.item(), a.any() or a.all().
任何想法为什么?我在我的代码中在不同的 pandas 数据帧上写了一个非常相似的 np.where 语句,它工作得非常好。不知道为什么它会在那里工作,但在这里却不行。
【问题讨论】:
-
您能否展示您的数据样本,以便我尝试重现此内容?
-
@MadPhysicist 是的,我刚刚添加了
-
np.where((...))双括号是故意的吗? -
@mkheifetz 我会让其他熟悉 numpy 并能准确解释发生了什么的人回答。我只是觉得双括号看起来很可疑。
-
不知道为什么您需要
np.where来获取简单的 0/1。请改用.astype(int)。
标签: python python-2.7 pandas numpy where