'Column' object is not callable with Regex and Pyspark

删除回忆录丶 提交于 2020-02-08 09:59:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!