2

How to replace empty string with \N in spark dataframe.

I tried the below one:

Df.na.replace(Seq("column1"),Map("" -> null)).na.fill("\N", Seq("column1"))

It's throwing me an error.

Sociopath
  • 12,395
  • 17
  • 43
  • 69
Bhaskar
  • 51
  • 2
  • 6

1 Answers1

5

You have to do like below

//Input df

+-----+-------+
| name|address|
+-----+-------+
|Manoj|Chennai|
|     |  Delhi|
|Alice|       |
+-----+-------+

//Replacement logic

df.na.replace(Seq("name","address"),Map(""->"\\n")).show

//Output df
+-----+-------+
| name|address|
+-----+-------+
|Manoj|Chennai|
|   \n|  Delhi|
|Alice|     \n|
+-----+-------+
Manoj Kumar Dhakad
  • 1,794
  • 1
  • 11
  • 24