从Apache Spark中的架构获取数据类型列表 [英] Get list of data types from schema in Apache Spark

查看:145
本文介绍了从Apache Spark中的架构获取数据类型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spark-Python中使用以下代码从DataFrame的架构中获取名称列表,这很好用,但是如何获取数据类型的列表呢?

I have the following code in Spark-Python to get the list of names from the schema of a DataFrame, which works fine, but how can I get the list of the data types?

columnNames = df.schema.names

例如,类似

columnTypes = df.schema.types

有没有办法获取DataFrame架构中包含的数据类型的单独列表?

Is there any way to get a separate list of the data types contained in a DataFrame schema?

推荐答案

以下是一个建议:

df = sqlContext.createDataFrame([('a', 1)])

types = [f.dataType for f in df.schema.fields]

types
> [StringType, LongType]

参考:

  • pyspark.sql.types.StructType
  • pyspark.sql.types.StructField

这篇关于从Apache Spark中的架构获取数据类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆