如何创建一个空的数据帧? [英] How to create an empty dataFrame?
本文介绍了如何创建一个空的数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要创建的数据帧在斯卡拉指定的架构。我曾尝试使用JSON读,我的意思是看空的文件,但我不认为这是最好的做法。
I want to create on DataFrame with a specified schema in Scala. I have tried to use JSon read, I mean reading empty file, but I don't think that's the best practice.
推荐答案
让我们假设你想用下面的模式的数据帧:
Lets assume you want a data frame with a following schema:
root
|-- k: string (nullable = true)
|-- v: integer (nullable = false)
您只需定义架构数据帧,并使用空的 RDD [行]
:
You simply define schema for a data frame and use empty RDD[Row]
:
import org.apache.spark.sql.types.{
StructType, StructField, StringType, IntegerType}
import org.apache.spark.sql.Row
val schema = StructType(
StructField("k", StringType, true) ::
StructField("v", IntegerType, false) :: Nil)
sqlContext.createDataFrame(sc.emptyRDD[Row], schema)
PySpark相当于几乎是相同的:
PySpark equivalent is almost identical:
from pyspark.sql.types import StructType, StructField, IntegerType, StringType
schema = StructType([
StructField("k", StringType(), True), StructField("v", IntegerType(), False)
])
# or df = sc.parallelize([]).toDF(schema)
df = sqlContext.createDataFrame([], schema)
这篇关于如何创建一个空的数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文