Created
October 11, 2016 14:10
-
-
Save samuelsmal/feb86d4bdd9a658c122a706f26ba7e1e to your computer and use it in GitHub Desktop.
PySpark DataFrame filtering using a UDF and Regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyspark.sql.functions import udf | |
from pyspark.sql.types import BooleanType | |
def regex_filter(x): | |
regexs = ['.*ALLYOURBASEBELONGTOUS.*'] | |
if x and x.strip(): | |
for r in regexs: | |
if re.match(r, x, re.IGNORECASE): | |
return True | |
return False | |
filter_udf = udf(regex_filter, BooleanType()) | |
df_filtered = df.filter(filter_udf(df.field_to_filter_on)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks , simple and straight foward