-2

I am new to Spark and Scala.

I have created a DataFrame from a csv in Spark. There is a column in the generated DataFrame which has a null value for some rows.

I want to check for this null value and replace it with a constant word say "Hello".

How can I do this in Spark?

Here is my sample code to create a dataframe from csv.

val DFCsv = spark.read.format("csv") .option("sep", ',') .option("inferSchema", "true") .option("header", "true") .load("/tmp/my.csv")
 DFCsv.show() 

Now one of the columns in that dataframe named "id" is null or empty/blank for some of the rows.

How can I iterate over each row one by one and then fill out column named "id" with a constant "Hello" String.

2

1 Answer 1

-2

One of the easiest way is to surround the null value with a Option and then do a pattern match on it.

Option(null) gets converted to None
Option(null).getOrElse("Hello)
Sign up to request clarification or add additional context in comments.

1 Comment

Here is my sample code to create a dataframe from csv. val DFCsv = spark.read.format("csv") .option("sep", ',') .option("inferSchema", "true") .option("header", "true") .load("/tmp/my.csv") DFCsv.show() Now one of the columns in that dataframe named "id" is null or empty/blank for some of the rows.How can I iterate over each row one by one and then fill out column named "id" with a constant "Hello" String.

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.