I have a dataframe DF1 that has three features (columns) a,b,c, all of StringType. I want to create a new dataframe DF2 from DF1 that has two columns:
b=c otherwise 0Input example:
a b c
A B B
B C A
D D D
Wanted output
a d
A 1
B 0
D 1
The part missing is drop for the other two columns.
val df2 = df1.withColumn("d", col("b") === col("c")).drop("b").drop("c")
df2.show
This gives us
+---+-----+
| a| d|
+---+-----+
| A| true|
| B|false|
| D| true|
+---+-----+
Please use This val df2=df1.withColumn("d",col("b") === col("c"))
Here WithColumn will add new columns in df2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With