I have a list of regex {"WeLove*", "Arizona.*hot", "Mahi*"}
and a dataframe with certain values that might match with one of the Regex expressions from the regex list.
| _c0 | _c1 | _c2 | _c3 |
|---|---|---|---|
| Arizona is hot | 2020 | 1 | Y |
| Arizona happens to be hot | 2020 | 1 | Y |
| MahiWalia | 2020 | 1 | Y |
| MahiSingh | 2020 | 1 | Y |
| MahiRandhawa | 2020 | 1 | Y |
| WeLovechocolate | 2020 | 1 | Y |
I am trying to find out for each regex how many matches are there in the data frame?
"WeLove*" = 1
"Arizona.*hot" = 2
"Mahi*" = 3
By doing this :
regex = spark.read.csv("regex.csv", header=False)
regex_list = regex.select("_c0").rdd.flatMap(lambda x: x).collect()
output = df.select("*", when(col("_c0").rlike("|".join(regex_list)), "True").alias("matches"))
filteredDf = output.filter(output.matches.isNotNull())
I was able to find the rows in dataFrame that at least matched with one of the regex in the list but I am not sure how to find which one and then group the rows according to the regex they matched with?