本文介绍了通过迭代列名称的Scala列表中的列从Spark数据框中删除多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据列,该列的列数约为400,我想根据需要删除100列.所以我创建了一个100列名称的Scala列表.然后,我想遍历for循环,以实际删除每个for循环迭代中的列.
I have a dataframe which has columns around 400, I want to drop 100 columns as per my requirement.So i have created a Scala List of 100 column names.And then i want to iterate through a for loop to actually drop the column in each for loop iteration.
下面是代码.
final val dropList: List[String] = List("Col1","Col2",...."Col100")
def drpColsfunc(inputDF: DataFrame): DataFrame = {
for (i <- 0 to dropList.length - 1) {
val returnDF = inputDF.drop(dropList(i))
}
return returnDF
}
val test_df = drpColsfunc(input_dataframe)
test_df.show(5)
推荐答案
答案:
val colsToRemove = Seq("colA", "colB", "colC", etc)
val filteredDF = df.select(df.columns .filter(colName => !colsToRemove.contains(colName)) .map(colName => new Column(colName)): _*)
这篇关于通过迭代列名称的Scala列表中的列从Spark数据框中删除多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!