gpt4 book ai didi

python - Pandas 逻辑运算

转载 作者:太空宇宙 更新时间:2023-11-04 08:44:26 25 4
gpt4 key购买 nike

我在使用 pandas 19.2 时遇到问题,无法得到预期的结果。 a-g 列有 ['yes','no','', NaN]。如果这些列中的任何一个为"is",我希望返回该行(未显示其他列)。这是我的代码。

xdf2  =  xdf[((xdf['a'] == 'yes').all() or 
(xdf['b'] == 'yes').all() or
(xdf['c'] == 'yes').all() or
(xdf['d'] == 'yes' ).all() or
(xdf['e'] == 'yes').all() or
(xdf['f'] == 'yes').all() or
(xdf['g'] =='yes').all()) ]

这给了我以下错误:

   2134                 return self._engine.get_loc(key)
2135 except KeyError:
-> 2136 return self._engine.get_loc(self._maybe_cast_indexer(key))
2137
2138 indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4433)()

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4279)()

pandas\src\hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13742)()

pandas\src\hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13696)()

KeyError: False

没有'.all'我得到

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

这似乎是一个简单而常见的代码片段,但我还没有找到一个很好的例子。我错过了什么?

最佳答案

尝试

xdf[xdf[list('abcdefg')].eq('yes').any(1)]

关于python - Pandas 逻辑运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42032091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com