createDataFrame

從 DataFrame、 一個列表、一個 RDD、 pandas.DataFrame、 或 numpy.ndarray一個 中建立 a pyarrow.Table 。

語法

createDataFrame(data, schema=None, samplingRatio=None, verifySchema=True)

參數

參數 類型 說明
data RDD 或可迭代 任何形式的 SQL 資料表示(Row如 、 tuple、 intbool、 、 dict等)的 RDD,或是 list、 pandas.DataFramenumpy.ndarraypyarrow.Table或 。
schema DataType、str 或 list,選用 A DataType、 資料型別字串,或欄位名稱的清單。 當提供欄位名稱清單時,會從 data推斷出每個欄位的類型。 當 None,從 推斷 data (需要 Row、 namedtuple、 或 dict)。 當 DataType 提供 OR 資料型態字串時,必須與實際資料相符。
samplingRatio 浮動,選用 當 data 為 RDD時,用於模式推論的列數取樣比率為 。 若 None,則使用前幾列。
verifySchema Bool,選用 驗證每一列的資料型態是否符合結構。 默認為啟用。 不支援 pyarrow.Table 輸入或啟用 Arrow 的 pandas 轉換。

退貨

DataFrame

Notes

使用 spark.sql.execution.arrow.pyspark.enabled=True 時還在實驗性質上。

Examples

# Create a DataFrame from a list of tuples.
spark.createDataFrame([('Alice', 1)]).show()
# +-----+---+
# |   _1| _2|
# +-----+---+
# |Alice|  1|
# +-----+---+

# Create a DataFrame from a list of dictionaries.
spark.createDataFrame([{'name': 'Alice', 'age': 1}]).show()
# +---+-----+
# |age| name|
# +---+-----+
# |  1|Alice|
# +---+-----+

# Create a DataFrame with column names specified.
spark.createDataFrame([('Alice', 1)], ['name', 'age']).show()
# +-----+---+
# | name|age|
# +-----+---+
# |Alice|  1|
# +-----+---+

# Create a DataFrame with an explicit schema.
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
schema = StructType([
    StructField("name", StringType(), True),
    StructField("age", IntegerType(), True)])
spark.createDataFrame([('Alice', 1)], schema).show()
# +-----+---+
# | name|age|
# +-----+---+
# |Alice|  1|
# +-----+---+

# Create a DataFrame with a DDL-formatted schema string.
spark.createDataFrame([('Alice', 1)], "name: string, age: int").show()
# +-----+---+
# | name|age|
# +-----+---+
# |Alice|  1|
# +-----+---+

# Create an empty DataFrame (schema is required when data is empty).
spark.createDataFrame([], "name: string, age: int").show()
# +----+---+
# |name|age|
# +----+---+
# +----+---+

# Create a DataFrame from Row objects.
from pyspark.sql import Row
Person = Row('name', 'age')
spark.createDataFrame([Person("Alice", 1)]).show()
# +-----+---+
# | name|age|
# +-----+---+
# |Alice|  1|
# +-----+---+