【发布时间】:2018-06-06 09:52:53
【问题描述】:
我在 pandas 中运行了一个小函数,当我运行 if x in y 语句时会引发 ValueError。我看到推荐布尔索引、.isin() 和where() 的类似问题,但我无法根据我的情况调整任何示例。任何建议将不胜感激。
附加说明:groups 是包含数据框之外的字符串的列表列表。我使用该函数的目标是查看数据框中的项目在哪个列表中,然后返回该列表的索引。我在下面的笔记本链接中的第一个版本使用iterrows 循环遍历数据帧,但我知道在大多数情况下这是次优的。
带有一些虚假数据的 Jupyter 笔记本:https://github.com/amoebahlan61/sturdy-chainsaw/blob/master/Grouping%20Test_1.1.ipynb
谢谢!
代码:
def groupFinder(item):
for group in groups:
if item in group:
return groups.index(group)
df['groupID2'] = groupFinder(df['item'])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-16-808ac3e51e1f> in <module>()
4 return groups.index(group)
5
----> 6 df['groupID2'] = groupFinder(df['item'])
<ipython-input-16-808ac3e51e1f> in groupFinder(item)
1 def groupFinder(item):
2 for group in groups:
----> 3 if item in group:
4 return groups.index(group)
5
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
953 raise ValueError("The truth value of a {0} is ambiguous. "
954 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 955 .format(self.__class__.__name__))
956
957 __bool__ = __nonzero__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
解决方案
我遇到了一些 pandas 的博客文章,还从 reddit 用户那里得到了一些反馈,这给了我一个解决方案,通过使用 pandas 的 apply 函数跳过使用 iterrows。
df['groupID2'] = df.item.apply(groupFinder)
感谢大家的帮助和回复。
【问题讨论】:
-
一般来说,在问题中包含指向数据的链接并不是一个好主意 - 链接可能会失效,一方面,另一方面,它更难帮助您。在这种情况下,单击您的链接 also 不会转到您的笔记本(尽管将 URL 字符串复制到浏览器中有效)。为了其他可能使用您的问题作为参考的人的利益,请考虑将您的示例数据和设置移动到您的帖子文本中,作为Minimal, Complete, and Verifiable Example。
-
@andrew_reece 感谢您提出问题和代码礼仪。我一定会继续使用它。
标签: python pandas numpy dataframe