“Column”对象不能用Regex和Pyspark调用

2024-04-25 02:17:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我只需要从“页面url”列中的url Sting中提取整数,并将这些提取的整数附加到新列中。我正在使用PySpark。我的代码如下:


from pyspark.sql.functions import col, regexp_extract

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

我有以下错误:TypeError:“Column”对象不可调用


Tags: 代码fromimporturlsqlextractcol整数
1条回答
网友
1楼 · 发布于 2024-04-25 02:17:36

你可以用

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

指定字符串列的名称作为^{}的第一个参数,并确保第三个参数设置为0,因为您的模式没有捕获组,并且您希望得到整个匹配值

请注意,当指定1作为第三个参数时,得到的结果为空:

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

相关问题 更多 >