Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use functions provide by DataFrameNaFunctions class in Spark, on a Dataframe?

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.

like image 777
Parth Vishvajit Avatar asked Apr 08 '16 12:04

Parth Vishvajit


1 Answers

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.

like image 126
eliasah Avatar answered Nov 03 '22 01:11

eliasah