通过排除使用 isin 过滤 pyspark 数据框 [英] Filtering a pyspark dataframe using isin by exclusion

查看:31
本文介绍了通过排除使用 isin 过滤 pyspark 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取数据框中的所有行,其中列值不在列表中(因此通过排除进行过滤).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆