'Column' 对象不能用 Regex 和 Pyspark 调用 [英] 'Column' object is not callable with Regex and Pyspark

查看:82
本文介绍了'Column' 对象不能用 Regex 和 Pyspark 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要从页面 URL"列中的 url 字符串中提取整数,并将这些提取的整数附加到新列中.我正在使用 PySpark.我的代码如下:

<预><代码>从 pyspark.sql.functions 导入 col, regexp_extractspark_df_url.withColumn("new_column", regexp_extract(col("页面 URL"), "\d+", 1).show())

我有以下错误:TypeError: 'Column' 对象不可调用.

解决方案

您可以使用

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

指定字符串列的名称作为 regexp_replace 并确保第三个参数设置为 0 因为您的模式没有捕获组并且您感兴趣结果得到整个匹配值.

注意,当你指定 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.

解决方案

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.

这篇关于'Column' 对象不能用 Regex 和 Pyspark 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆