熊猫逻辑运算

时间:2017-02-03 19:57:13

标签: python pandas

我遇到了大熊猫的问题19.2给了我预期的结果。列a-g具有['是',' no','',NaN]。如果这些列中的任何一列都有“是”和“#39;我想要返回的行(还有其他列未显示)。这是我的代码。

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().

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

2 个答案:

答案 0 :(得分:5)

尝试

double y = inputY.nextDouble();
int x = (int) y;

答案 1 :(得分:0)

以下内容应该有效:

import pandas as pd

a = [["yes", "no", "yes", "yes"],
     ["yes", "yes", "no", "yes"],
     ["yes", "no", "yes", "yes"]]
xdf = pd.DataFrame(a, columns=["a", "b", "c", "d"])     
print xdf

boollist = [ (xdf[col] == "yes").all() for col in xdf.columns ]
xdf2  =  xdf[xdf.columns[boollist] ]
print xdf2