通过排除使用isin过滤pyspark数据帧 [英] Filtering a pyspark dataframe using isin by exclusion
问题描述
我正在尝试获取数据框中的所有行,其中列值不在列表中(因此通过排除进行过滤).
I am trying to get all rows within a dataframe where a columns value is not within a list (so filtering by exclusion).
例如:
df = sqlContext.createDataFrame([('1','a'),('2','b'),('3','b'),('4','c'),('5','d')]
,schema=('id','bar'))
我得到了数据框:
+---+---+
| id|bar|
+---+---+
| 1| a|
| 2| b|
| 3| b|
| 4| c|
| 5| d|
+---+---+
我只想排除bar为('a'或'b')的行.
I only want to exclude rows where bar is ('a' or 'b').
使用SQL表达式字符串将是:
Using an SQL expression string it would be:
df.filter('bar not in ("a","b")').show()
有没有一种方法可以不使用字符串作为SQL表达式,或者一次不包含一项?
Is there a way of doing it without using the string for the SQL expression, or excluding one item at a time?
我很可能会列出我想使用的排除值.['a','b'].
I am likely to have a list, ['a','b'], of the excluded values that I would like to use.
推荐答案
看起来〜提供了我需要的功能,但是我还没有找到合适的文档.
It looks like the ~ gives the functionality that I need, but I am yet to find any appropriate documentation on it.
df.filter(~col('bar').isin(['a','b'])).show()
+---+---+
| id|bar|
+---+---+
| 4| c|
| 5| d|
+---+---+
这篇关于通过排除使用isin过滤pyspark数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!