Created
September 4, 2019 20:38
-
-
Save tonyfraser/e9b6fb6a2cb7ceb216dc2261d30c5752 to your computer and use it in GitHub Desktop.
dynamically create a column if a column in a spark dataframe if it does not already exist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//An example of dynamically adding a column if it does not exist | |
val df = Seq( | |
("channel_one", "my_show", "episode1"), | |
("channel_one", "my_show", "episode2") | |
).toDF("network_name", "show_name", "episode") | |
//there is no rank column so add one | |
val newdf = df.columns match { | |
case a if a contains "rank" => df | |
case _ =>df.withColumn("rank", lit("0")) | |
} | |
newdf.show | |
// +------------+---------+--------+----+ | |
// |network_name|show_name| episode|rank| | |
// +------------+---------+--------+----+ | |
// | channel_one| my_show|episode1| 0| | |
// | channel_one| my_show|episode2| 0| | |
// +------------+---------+--------+----+ | |
// there is a show_name column | |
val newdf2 = df.columns match { | |
case a if a contains "show_name" => df | |
case _ =>df.withColumn("rank", lit("0")) | |
} | |
newdf2.show | |
// +------------+---------+--------+ | |
// |network_name|show_name| episode| | |
// +------------+---------+--------+ | |
// | channel_one| my_show|episode1| | |
// | channel_one| my_show|episode2| | |
// +------------+---------+--------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment