从 Apache Spark 中的模式获取数据类型列表 [英] Get list of data types from schema in Apache Spark
本文介绍了从 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]
参考:
这篇关于从 Apache Spark 中的模式获取数据类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文