我有一个pyspark框架,基本上看起来像下表:
| 产品|名称|
| --|--|
| ABCD - 12| ABCD|
| xyz - 123543| xyz|
我希望创建一个新列(UPC),它只包含Product列中连字符右侧的数字。
我知道在Excel中我可以使用Right函数和len和find,但据我所知,这些在Python中没有等价物。
我尝试创建两个新列,LastHyphen(因为product列可能有超过1个连字符)和ProductLength。然后我希望将它们插入子字符串函数,但我一直得到“列不可迭代”错误。
df4 = df3.withColumn("LastHyphen",length(col("PRODUCT"))-locate('-',reverse(col("PRODUCT"))))
df4 = df4.withColumn("ProductLength",length(col("PRODUCT")))
df4 = df4.withColumn("UPC", substring("PRODUCT", df4.LastHyphen, df4.ProductLength - df4.LastHyphen))
TypeError: Column is not iterable
字符串
我希望得到这样的输出:
| 产品|UPC|
| --|--|
| ABCD - 12| 12 |
| xyz - 123543| 123543 |
1条答案
按热度按时间ssgvzors1#
有一个类似的问题here,答案涉及到一个regexp拆分。
在您的特定环境中,使用正则表达式从字符串中提取UPC可能是最简单的方法。
个字符