我有一个大的 pyspark.sql.dataframe.DataFrame
,我想保留(所以 filter
)保存在 location
列中的 URL 包含预定字符串的所有行,例如“google.com”。
我努力了:
import pyspark.sql.functions as sf
df.filter(sf.col('location').contains('google.com')).show(5)
但这会引发
TypeError: _TypeError: 'Column' object is not callable'
如何正确过滤我的df?提前谢谢了!
Spark 2.2 及以上版本
df.filter(df.location.contains('google.com')) Spark 2.2 文档链接
Spark 2.1 及之前版本
您可以在过滤器 df.filter("location like '%google.com%'") 或使用 DataFrame 列方法 df.filter(df.location.like('%google.com%')) Spark 2.1 文档中使用普通 SQL关联
pyspark.sql.Column.contains()
仅在 pyspark 2.2 及更高版本中可用。
df.where(df.location.contains('google.com'))
.contains()
中给出多个字符串
在使用字符串值过滤 DataFrame 时,我发现 pyspark.sql.functions
lower
和 upper
会派上用场,如果您的数据可能包含“foo”和“Foo”等列条目:
import pyspark.sql.functions as sql_fun
result = source_df.filter(sql_fun.lower(source_df.col_name).contains("foo"))
like
的google.com
和amazon.com
,该怎么办?我们怎么能这样做?df.filter("location like '%google.com%' AND location like '%amazon.com%'")
或 DataFramedf.filter("location like '%google.com%'").filter("location like '%amazon.com%'")
df.filter(F.col("yourcol").rlike('|'.join(substrings)))
其中 substrings 是子字符串列表,例如substrings = ['google.com','amazon.com']