2016-12-20 21 views
1

すべての列の値がゼロであるPySparkデータフレームの行をフィルタリングしようとしています。PySpark DataFrameフィルタを使用して条件のリストを論理的にAND

私はこのようなものを使用することを願っていた。

from pyspark.sql.functions import col 
df.filter(all([(col(c) != 0) for c in df.columns])) 

をしかし、私はValueErrorを取得:

ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions. 

論理と条件のリスト上を実行する方法はありますか?

答えて

4

述語

from pyspark.sql.functions import lit 
from operator import and_ 
from functools import reduce 

df.where(reduce(and_, (col(c) != 0 for c in df.columns))) 

または

df.where(reduce(and_, (col(c) != 0 for c in df.columns), lit(True))) 

のちょうどreduceリストあなたは述語のリストが空であるかもしれないことを期待しています。例えば

データは次のように見える場合:

df = sc.parallelize([ 
    (0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 1) 
]).toDF(["x", "y", "z"]) 

結果は次のようになります。

+---+---+---+ 
| x| y| z| 
+---+---+---+ 
| 1| 1| 1| 
+---+---+---+ 
関連する問題