0
假設我有一個數據幀df
,它有多個列column1
,column2
..我想查找每列中空值的計數。做到這一點的方法是從每一列如:計算dataframe中的空值:scala spark
df.filter($"column1" !== "").count
df.filter($"column2" !== "").count
.
.
但是有沒有辦法做到一行?
假設我有一個數據幀df
,它有多個列column1
,column2
..我想查找每列中空值的計數。做到這一點的方法是從每一列如:計算dataframe中的空值:scala spark
df.filter($"column1" !== "").count
df.filter($"column2" !== "").count
.
.
但是有沒有辦法做到一行?
我會建議使用某種聚集,並建立一個新的數據框:
df.agg(
sum(when($"column1" !== "", 1).otherwise(0)),
sum(when($"column2" !== "", 1).otherwise(0))
)
編輯:如果你有很多欄目,嘗試這樣的事情:
val ncols=2
val cols = (1 to ncols)
.map(i=> sum(when(col("column"+i)!=="",1).otherwise(0)))
df.agg(cols.head,cols.tail:_*)
儘管此代碼片段可能會解決問題,[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高您的帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 –
謝謝你的答案,但我的主要問題是如何避免寫'column1','column2' ...不能使用這個代碼,如果我有100列或更多。 –
@MpizosDimitris查看我更新的答案 –