Reputation: 73
This is a sample of my original dataframe df
+----+
| mix|
+----+
| 1|
| 2|
| cap|
| 3|
| 53|
| 56|
| 98|
| 90|
+----+
The current datatype of the column is StringType
After replacing the value cap
with 0
, there could be two cases
StringType
How to infer datatype again so i can know if the column is purely numerical after replacement or not.And if Numerical
, what is the exact datatype viz Integer
, Float
,Double
df.withColumn("mix",when(col("mix") === "cap",0).otherwise(col("mix")))
Upvotes: 0
Views: 231
Reputation: 409
val mix = Seq("1","2","cap","4").toDF("mix")
mix.printSchema()
root
|-- mix: string (nullable = true)
val after = mix.withColumn("mix",when(col("mix") === "cap",0).otherwise(col("mix")).cast(IntegerType))
after.printSchema()
root
|-- mix: integer (nullable = true)
Let me know, if this helps you.
Upvotes: 1