0

i am new to spark dataframe .i have a text file having data like

schoolid,classid,studentid,subject,marks
bjs,5,111,hindi,23
bjs,5,222,maths,78
bjs,7,333,bio,89
bjs,1,444,chemistry,67
ghs,2,555,bio,78
ghs,2,666,phy,56
ghs,9,777,drawing,56

i want to convert this data to dataframe and add 1 to each of the values under marks column

so code I am using is

val df = sparkSession.read.format("csv").option("header","true").load("samplefile1.txt")
 val newdf = df.select(col($"marks"+1)).show()

but the error that I am getting is

type mismatch; found : org.apache.spark.sql.Column required: String

can I have help with the correct syntax

2 Answers 2

3

Try this solution:

df.withColumn("marks",col("marks") + lit(1)).show
Sign up to request clarification or add additional context in comments.

Comments

2
 df.withColumn("marks", expr("marks +1").cast("integer")).show

Output :

+--------+-------+---------+---------+-----+
|schoolid|classid|studentid|  subject|marks|
+--------+-------+---------+---------+-----+
|     bjs|      5|      111|    hindi|   24|
|     bjs|      5|      222|    maths|   79|
|     bjs|      7|      333|      bio|   90|
|     bjs|      1|      444|chemistry|   68|
|     ghs|      2|      555|      bio|   79|
|     ghs|      2|      666|      phy|   57|
|     ghs|      9|      777|  drawing|   57|
+--------+-------+---------+---------+-----+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.