如何替换DataFrame中Field1列的空值?调试无效代码求助
Got it, let's break down why your current command isn't working and get you the expected result!
The Problem with Your Current Code
You mixed up the parameter order in the na.fill method. Spark's fill function expects the fill value as the first argument, followed by the sequence of column names you want to apply the fill to. Your code df.na.fill("Field1",Seq("Anonymous")) is trying to fill a column named "Anonymous" with the value "Field1"—which is totally the opposite of what you need!
Correct Solutions
Here are two straightforward ways to fix this:
Option 1: Using Value + Column Sequence
Specify the fill value first, then the target column(s):
val filledDf = df.na.fill("Anonymous", Seq("Field1"))
Option 2: Using a Map (Great for Multiple Columns)
If you ever need to fill different columns with different values, a map is cleaner. For your single column case, it looks like this:
val filledDf = df.na.fill(Map("Field1" -> "Anonymous"))
Expected Output
After running either of the above commands, your DataFrame will look exactly like you wanted:
Field1 Field2 Anonymous AA 12 BB
内容的提问来源于stack exchange,提问作者ScalaBoy

