如何按条件将一个spark dataframe列拆分为两列

如何按条件将一个spark dataframe列拆分为两列,dataframe,apache-spark,pyspark,Dataframe,Apache Spark,Pyspark,我想替换pyspark dataframe的一列 数据帧: price 90.16|USD 我需要: dollar_price currency 9016 USD Pypark代码: new_col = F.when(F.col("price").isNull() == False, F.substring(F.col('price'), 1, F.instr(F.col('retail_value'), '|')-1)).o

我想替换pyspark dataframe的一列

数据帧:

   price
   90.16|USD  
我需要:

  dollar_price currency
  9016          USD
Pypark代码:

  new_col = F.when(F.col("price").isNull() == False, F.substring(F.col('price'), 1, F.instr(F.col('retail_value'), '|')-1)).otherwise(null)


   new_df = df.withColumn('dollar_price', new_col)

   new_col = F.when(F.col("price").isNull() == False, F.substring(F.col('price'), F.instr(F.col('retail_value'), '|')+1, 3)).otherwise(null)


   new_df_1 = new_df.withColumn('currency', new_col)
我得到一个错误:

  TypeError: Column is not iterable
你能告诉我我错过了什么吗

我试过了

但它不起作用


感谢在您从
instr
函数计算值时,尝试使用
expr

示例:

df.show()
#+---------+
#|    price|
#+---------+
#|90.16|USD|
#+---------+

from pyspark.sql.functions import *
from pyspark.sql.types import *

df.withColumn("dollar_price",when(col("price").isNull()==False,expr("substring(price,1,instr(price,'|')-1)")).otherwise(None)).\
withColumn("currency",when(col("price").isNull()==False,expr("substring(price,instr(price,'|')+1,3)")).otherwise(None)).\
show()

#+---------+------------+--------+
#|    price|dollar_price|currency|
#+---------+------------+--------+
#|90.16|USD|       90.16|     USD|
#+---------+------------+--------+

为什么不能使用
df.selectExpr(“分割(价格)”[0]作为美元价格”,“分割(价格)”[1]作为货币”)