score:1
Accepted answer
Chaining multiple when
expressions on id
column values:
val ids = Seq(1, 3, 4, 7)
val scoreCol = ids.foldLeft(lit(null)) { case (acc, id) =>
when(col("sourceId")===id, col(s"score_$id")).otherwise(acc)
}
val df2 = df.withColumn("score", scoreCol)
Or building a map expression from score_*
columns and use it to get score
values:
val scoreMap = map(
df.columns
.filter(_.startsWith("score_"))
.flatMap(c => Seq(lit(c.split("_")(1)), col(c))): _*
)
val df2 = df.withColumn("score", scoreMap(col("sourceId")))
Source: stackoverflow.com
Related Query
- Select a column based on another column's value in Spark Dataframe using Scala
- Select columns from a dataframe into another dataframe based on column datatype in Apache Spark Scala
- Adding new Columns based on aggregation on existing column in Spark DataFrame using scala
- Check if value from one dataframe column exists in another dataframe column using Spark Scala
- Calculating column value in current row of Spark Dataframe based on the calculated value of a different column in previous row using Scala
- Scala Spark dataframe filter using multiple column based on available value
- How to select specific columns from Spark DataFrame based on the value of another column?
- splitting string column into multiple columns based on key value item using spark scala
- How to compose column name using another column's value for withColumn in Scala Spark
- Convert row values into columns with its value from another column in spark scala
- Convert multiple columns into a column of map on Spark Dataframe using Scala
- Update column value from another columns based on multiple conditions in spark structured streaming
- dataframe columns as key and column data as value group by id in spark scala
- Spark Dataframe with pivot and different aggregation, based on the column value (measure_type) - Scala
- Convert DataType of all columns of certain DataType to another DataType in Spark DataFrame using Scala
- Drop rows of Spark DataFrame that contain specific value in column using Scala
- Update a column value in a spark dataframe based another column
- Scala spark - count null value in dataframe columns using accumulator
- How to create a column of row id in Spark dataframe for each distinct column value using Scala
- How to find position of substring in another column of dataframe using spark scala
- Iterate a column values in a Stream dataframe and assign each value to a common list using Scala and Spark
- How to create new columns in dataframe using Spark Scala based on different string patterns
- Scala Spark Dataframe Create a new column with maximum of previous and current value of another column
- Select Columns in Spark Dataframe based on Column name pattern
- How to fill column with value taken from a (non-adjacent) previous row without natural partitioning key using Spark Scala DataFrame
- Find average value from a column of stream dataframe with array values using spark scala
- Scala Spark creating a new column in the dataframe based on the aggregate count of values in another column
- Scala Spark Incrementing a column based on another column in dataframe without for loops
- Spark dataframe duplicate row based on splitting column value in scala
- Reduce rows in a group based on a column value using Spark / Scala
More Query from same tag
- Macros annotation not working in IntelliJ14
- check for null values in Option[Long]
- Kafka - How to read more than 10 rows
- How to merge all unique values of a spark dataframe column into single row based on id and convert the column into json format
- How to get Future[Seq[Person]] instead of Seq[Future[Person]]
- Describe recursive grammar with type aliases
- Add Tuple Object into List Scala
- Is there a way to skip/throw-out/ignore records in Spark during a map?
- Merging nested list of objects into a single list
- Martin Odersky : Working hard to keep it simple
- How to convert json to string with escape characters using json4s
- pass typesafe config file in maven
- Type parameter in scala
- Can't launch from an object
- How do I sign my ProGuard'ed Scala stand-alone JARs?
- How to convert rdd object to dataframe in spark
- Mockito stubbing method with value class argument fails with NullPointerException
- convert scala string to RDD[seq[string]]
- Unable to remove empty strings in scala list
- Fixing case object identity / pattern matching under serialization
- Using lots of mixins with self type
- Automatic null assert in scala
- Convert current time in milliseconds to Date Time format in Scala
- select query of any table using spark.sql giving zero records sometimes but same select query giving result in HIVE CLI
- scala - extract substring based on special characters
- java.lang.StackOverflowError throw in spark-submit but not in running in IDE
- how to read a file from a lift webapp
- Merge two LinkedHashMap in Scala
- How to build the tree if I know nodes and their parents?
- Enrich-my-library by extending TraversableLike with own methods