Dataframe Pyspark列转换同时具有正数和负数的字符串
我有一个pysapark列,它是字符串数据类型。但它有正数和负数。如何将它们转换为数字 总金额=数量*价格 当前数量=字符串,价格=双倍 预期输出:Dataframe Pyspark列转换同时具有正数和负数的字符串,dataframe,pyspark,casting,Dataframe,Pyspark,Casting,我有一个pysapark列,它是字符串数据类型。但它有正数和负数。如何将它们转换为数字 总金额=数量*价格 当前数量=字符串,价格=双倍 预期输出: Volume price_perunit total amount -0.75 100 -75 8 100 800 -0.01 8 -0.08 现在,当我乘法时,我得到了以下结果,这是错误的,因为
Volume price_perunit total amount
-0.75 100 -75
8 100 800
-0.01 8 -0.08
现在,当我乘法时,我得到了以下结果,这是错误的,因为负号不再可用
Volume price_perunit total amount
-0.75 100 75
8 100 800
-0.01 8 0.08
只是铸造
df.printSchema()
root
|-- Volume: string (nullable = true)
|-- price_perunit: double (nullable = true)
df.withColumn('Volume', col('Volume').cast('double')) \
.withColumn('total amount', expr('Volume * price_perunit')) \
.show()
+------+-------------+------------+
|Volume|price_perunit|total amount|
+------+-------------+------------+
| -0.75| 100| -75.0|
| 8.0| 100| 800.0|
| -0.01| 8| -0.08|
+------+-------------+------------+