我有一个火花数据帧
inputDF: org.apache.spark.sql.DataFrame = [_id: string, Frequency: double, Monterary: double, Recency: double, CustID: string]
root
|-- _id: string (nullable = false)
|-- Frequency: double (nullable = false)
|-- Monterary: double (nullable = false)
|-- Recency: double (nullable = false)
|-- CustID: string (nullable = false)
我想通过从中删除字符串列来创建一个新的数据帧。具体条件是不循环访问列名。有人知道吗?
如果架构是平面的并且只包含简单类型,则可以筛选字段,但除非你有水晶球,否则您无法真正避免迭代:
import org.apache.spark.sql.types.StringType
import org.apache.spark.sql.functions.col
df.select(df.schema.fields.flatMap(f => f.dataType match {
case StringType => Nil
case _ => col(f.name) :: Nil
}): _*)