createDataFrame

Tworzy obiekt DataFrame na podstawie RDDlisty , pandas.DataFrame, , lub numpy.ndarraypyarrow.Table.

Składnia

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

Parametry

Parameter Typ Opis
data RDD lub iterowalny RDD dowolnego rodzaju reprezentacji danych SQL (Row, tuple, int, booldict, itp.) lub list, pandas.DataFrame, numpy.ndarraylub pyarrow.Table.
schema Typ danych, str lub lista, opcjonalnie Ciąg DataTypetypu danych lub lista nazw kolumn. Po podaniu listy nazw kolumn typ każdej kolumny jest wywnioskowany z .data Gdy Noneschemat jest wywnioskowany z data elementu (wymaga Row, namedtuplelub dict). DataType Po podaniu ciągu typu danych lub musi być zgodny z rzeczywistymi danymi.
samplingRatio zmiennoprzecinkowy, opcjonalny Przykładowy stosunek wierszy używanych do wnioskowania schematu, gdy data jest .RDD Jeśli Nonezostanie użytych kilka pierwszych wierszy.
verifySchema wartość logiczna, opcjonalnie Zweryfikuj typy danych każdego wiersza względem schematu. Włączone domyślnie. Nieobsługiwane w przypadku konwersji biblioteki pandas z obsługą pyarrow.Table wprowadzania lub strzałki.

Zwroty

DataFrame

Notatki

Użycie za pomocą spark.sql.execution.arrow.pyspark.enabled=True polecenia jest eksperymentalne.

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|
# +-----+---+