I have a dataframe and I want to use one of the replace() function of
org.apache.spark.sql.DataFrameNaFunctions
on that dataframe.
Problem: I don't get these methods in intelligence (suggestions) with dataframe's instance. I imported that class explicitly.
I am not able to find any stuff which can give me some demonstration of how to use these functions or how to cast dataframe to type of DataFrameNaFunctions
.
I tried to cast it using asInstanceof[]
method but it throws exception.
This can be a bit confusing but it's quite straightforward to be honest. Here is an small example :
scala> val df = sqlContext.read.format("com.databricks.spark.csv").option("header","true").option("inferSchema","true").load("na_test.csv")
// df: org.apache.spark.sql.DataFrame = [name: string, age: int]
scala> df.show()
// +-----+----+
// | name| age|
// +-----+----+
// |alice| 35|
// | bob|null|
// | | 24|
// +-----+----+
scala> df.na.fill(10.0,Seq("age"))
// res4: org.apache.spark.sql.DataFrame = [name: string, age: int]
// scala> df.na.fill(10.0,Seq("age")).show
// +-----+---+
// | name|age|
// +-----+---+
// |alice| 35|
// | bob| 10|
// | | 24|
// +-----+---+
scala> df.na.replace("age", Map(35 -> 61,24 -> 12))).show()
// +-----+----+
// | name| age|
// +-----+----+
// |alice| 61|
// | bob|null|
// | | 12|
// +-----+----+
To access org.apache.spark.sql.DataFrameNaFunctions
you can call .na.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With