1

I'm trying run a Regex in Python on a dataframe in Apache Spark.

The df is

enter image description here

The regex is as follows:

import re
m = re.search("[Pp]ython", df)
print(m)

I'm getting the following error message:

TypeError: expected string or bytes-like object

The following will work

import re m = re.search("[Pp]ython", 'Python python') print(m)

But I would like the regex to work on a dataframe

1 Answer 1

1

You can use regexp_extract:

from pyspark.sql import functions as F

data = [["Python"],["python"], ["Scala"], ["PYTHON"]]
schema= ["language"]

df = spark.createDataFrame(data, schema)

df = df.withColumn("extracted", F.regexp_extract("language", "[Pp]ython", 0))

Result:

+--------+---------+
|language|extracted|
+--------+---------+
|  Python|   Python|
|  python|   python|
|   Scala|         |
|  PYTHON|         |
+--------+---------+

The definition for re.search is

re.search(pattern, string, flags=0)

The second parameter being a string, this function cannot work with Spark dataframes. However (at least most) patterns that work with re.search will also work for regexp_extract. So testing the patterns with re.search first might be a way.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for getting touch. That's a great solution, however I'm a real novice with RegEx. I'm testing using re.search() with import re. Is it not possible to use re.search() with a dataframe?
@Patterson unfortunately no. re.search works on a string and you are looking for a solution that works on a column of a dataframe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.