Reputation: 779
I have a databricks scala notebook. I am writing the following
var df=(some code to create the dataframe)
if (STRING1 == "testing"){
var df2=df.withColumn("New",lit("astring"))
}
df2.show()
and seems like I am not able to show df2, and df2 must be declare first ... how can I do that ?
Upvotes: 0
Views: 170
Reputation: 717
The problem is when you declare a variable inside the if statement, the scope is only available till the end of the if block.
So you could either move the df2 within the if block
var df=(some code to create the dataframe)
if (STRING1 == "testing"){
var df2=df.withColumn("New",lit("astring"))
df2.show()
}
or just declare df2 outside of the if statement
var df=(some code to create the dataframe)
var df2= if (STRING1 == "testing"){
df.withColumn("New",lit("astring"))
} else df
df2.show()
Upvotes: 1