作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 CSV 输入文件。我们使用以下内容阅读
val rawdata = spark.
read.
format("csv").
option("header", true).
option("inferSchema", true).
load(filename)
这会巧妙地读取数据并构建架构。
下一步是将列拆分为字符串和整数列。怎么办?
如果以下是我的数据集的架构...
scala> rawdata.printSchema
root
|-- ID: integer (nullable = true)
|-- First Name: string (nullable = true)
|-- Last Name: string (nullable = true)
|-- Age: integer (nullable = true)
|-- DailyRate: integer (nullable = true)
|-- Dept: string (nullable = true)
|-- DistanceFromHome: integer (nullable = true)
我想将其分成两个变量(StringCols、IntCols),其中:
这是我试过的:
val names = rawdata.schema.fieldNames
val types = rawdata.schema.fields.map(r => r.dataType)
现在在 types
中,我想循环查找所有 StringType
并在名称中查找列名,对于 IntegerType
也是如此.
最佳答案
在这里,您可以使用底层 schema
和 dataType
import org.apache.spark.sql.types.{IntegerType, StringType}
val stringCols = df.schema.filter(c => c.dataType == StringType).map(_.name)
val intCols = df.schema.filter(c => c.dataType == IntegerType).map(_.name)
val dfOfString = df.select(stringCols.head, stringCols.tail : _*)
val dfOfInt = df.select(intCols.head, intCols.tail : _*)
关于scala - 如何将每种类型的列分成两组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44368278/
我是一名优秀的程序员,十分优秀!