问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/59865668/column-object-is-not-callable-with-regex-and-pyspark