score:1
Accepted answer
That happens because you use lit
. You should use col
instead:
df
.withColumn(
"Rate" ,when(col("nodes_count") =!= 0,
(col("edges_count") / col("nodes_count")).as[Double]).otherwise(0.0))
although both when
and as Double
are useless here, and simple division would be more than sufficient:
df.withColumn("Rate", col("edges_count") / col("nodes_count"))
Source: stackoverflow.com
Related Query
- New column receives the value Null
- Spark, add new Column with the same value in Scala
- Add a column to spark dataframe which contains list of all column names of the current row whose value is not null
- replace null value in the column of a dataframe with the value in other dataframe wrt to id
- The Spark UDF is not changing the column value from null to 0
- Fetch the partial value from a column having key value pairs and assign it to new column in Spark Dataframe
- Scala DataFrame, copy value of non null column into a new column
- how to concat the same column value to a new column with comma delimiters in spark
- Adding a new column to a dataframe with a value which is based on the values from next rows
- how to add a new column having the value of the following line with scala dataframe
- Add new Column with the constant value as list
- Create a new column from one of the value available in another columns as an array of Key Value pair
- Create DataFrame with null value for few column
- Getting the value of a DataFrame column in Spark
- [SlickException: Read NULL value for column (USERS /670412212).LOGIN_ID]
- How can I use the new Slick 2.0 HList to overcome 22 column limit?
- Spark dataframes: Extract a column based on the value of another column
- Play: How to remove the fields without value from JSON and create a new JSON with them
- Group by column "grp" and compress DataFrame - (take last not null value for each column ordering by column "ord")
- Adding a new column to a Dataframe by using the values of multiple other columns in the dataframe - spark/scala
- Finding the difference of two columns in Spark dataframes and appending to a new column
- How Scala Array apply method returns the value at the index when the implementation is simply throw new error
- Scala Spark, how to add value to the column
- Spark: Replace Null value in a Nested column
- How to get the Integer value of a column in .withColumn function? [Spark - Scala]
- Find for each row the first non-null value in a group of columns and the column name
- How to fetch the value and type of each column of each row in a dataframe?
- Calculate value based on value from same column of the previous row in spark
- adding column with the length of other column as value using scala
- Fill null values in dataframe column with next value
More Query from same tag
- Is there a better way to know if the current read line is null in a read-while loop?
- Generate sentences usign streams using scala
- POJO's in Scala
- Spark - subset dataset based on occurences of user id
- an error in spark sbt assembly
- ScalaMock, returns based on a ClassTag
- Cannot add scala swing dependency via Gradle in Scala IDE
- Scala - Use predicate function to summarize list of strings
- Why List.fill method has two group of parameters instead of one?
- How to load a class from the source code using reflection inside SBT task?
- scala variables initialization
- Select max common Date from differents DataFrames (Scala Spark)
- Warnings while executing a function in a function
- Write RDD[OmnitureData] to S3
- What is an entity in Akka-Http?
- Why can't sbt find my local package
- Not able to connect Spark-Cloudant
- Multiple scala versions in the same project
- Two objects having same hashcode means they are referring to same object ..right?
- How to match on unknown generic type without warnings
- scala iterate java hashSet
- How to get Squeryl to release closed connections back to C3P0?
- structural type with upper bound?
- scalac -explaintypes: What does "<notype> <: X?" mean?
- Why can't the compiler infer type parameter for package object (while it works fine for object)?
- Scala regex "starts with lowercase alphabets" not working
- Image gives NULL when passing from service to controller using MultipartFile in spring
- ScalaPB not working when trying to compile SBT project. Compilation fails with "object gen is not a member of package scalapb"
- How to calculate the mean of each pair in an RDD consisting of (Key, [Value]) pairs in Spark?
- spark-cassandra (java.lang.NoClassDefFoundError: org/apache/spark/sql/cassandra/package)