-1

I need to extract the integers only from url stings in the column "Page URL" and append those extracted integers to a new column. I am using PySpark. My code below:


from pyspark.sql.functions import col, regexp_extract

spark_df_url.withColumn("new_column", regexp_extract(col("Page URL"), "\d+", 1).show())

I have the following error: TypeError: 'Column' object is not callable.

Chique_Code
  • 1,422
  • 3
  • 23
  • 49

1 Answers1

2

You may use

spark_df_url.withColumn("new_column", regexp_extract("Page URL", "\d+", 0))

Specify the name of the string column as the first argument to regexp_replace and make sure the third argument is set to 0 as your pattern has no capturing groups and you are interested in getting the whole match value as a result.

Note that when you specified 1 as the third argument, you got empty results:

If the regex did not match, or the specified group did not match, an empty string is returned.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563