pyspark.sql.SQLContext.createDataFrame#
- SQLContext.createDataFrame(data, schema=None, samplingRatio=None, verifySchema=True)[source]#
Creates a
DataFramefrom anRDD, a list, apandas.DataFrame, or apyarrow.Table.When
schemais a list of column names, the type of each column will be inferred fromdata.When
schemaisNone, it will try to infer the schema (column names and types) fromdata, which should be an RDD ofRow, ornamedtuple, ordict.When
schemaispyspark.sql.types.DataTypeor a datatype string it must match the real data, or an exception will be thrown at runtime. If the given schema is notpyspark.sql.types.StructType, it will be wrapped into apyspark.sql.types.StructTypeas its only field, and the field name will be “value”, each record will also be wrapped into a tuple, which can be converted to row later.If schema inference is needed,
samplingRatiois used to determine the ratio of rows used for schema inference. The first row will be used ifsamplingRatioisNone.Added in version 1.3.0.
Changed in version 2.0.0: The
schemaparameter can be apyspark.sql.types.DataTypeor a datatype string after 2.0. If it’s not apyspark.sql.types.StructType, it will be wrapped into apyspark.sql.types.StructTypeand each record will also be wrapped into a tuple.Changed in version 2.1.0: Added verifySchema.
Changed in version 4.0.0: Added support for
pyarrow.Table.- Parameters:
- data
RDDor iterable an RDD of any kind of SQL data representation (
Row,tuple,int,boolean, etc.), orlist,pandas.DataFrame, orpyarrow.Table.- schema
pyspark.sql.types.DataType, str or list, optional a
pyspark.sql.types.DataTypeor a datatype string or a list of column names, default is None. The data type string format equals topyspark.sql.types.DataType.simpleString, except that top level struct type can omit thestruct<>.- samplingRatiofloat, optional
the sample ratio of rows used for inferring
- verifySchemabool, optional
verify data types of every row against schema. Enabled by default.
- data
- Returns:
Examples
>>> l = [('Alice', 1)] >>> sqlContext.createDataFrame(l).collect() [Row(_1='Alice', _2=1)] >>> sqlContext.createDataFrame(l, ['name', 'age']).collect() [Row(name='Alice', age=1)]
>>> d = [{'name': 'Alice', 'age': 1}] >>> sqlContext.createDataFrame(d).collect() [Row(age=1, name='Alice')]
>>> rdd = sc.parallelize(l) >>> sqlContext.createDataFrame(rdd).collect() [Row(_1='Alice', _2=1)] >>> df = sqlContext.createDataFrame(rdd, ['name', 'age']) >>> df.collect() [Row(name='Alice', age=1)]
>>> from pyspark.sql import Row >>> Person = Row('name', 'age') >>> person = rdd.map(lambda r: Person(*r)) >>> df2 = sqlContext.createDataFrame(person) >>> df2.collect() [Row(name='Alice', age=1)]
>>> from pyspark.sql.types import * >>> schema = StructType([ ... StructField("name", StringType(), True), ... StructField("age", IntegerType(), True)]) >>> df3 = sqlContext.createDataFrame(rdd, schema) >>> df3.collect() [Row(name='Alice', age=1)]
>>> sqlContext.createDataFrame(df.toPandas()).collect() [Row(name='Alice', age=1)] >>> sqlContext.createDataFrame(pandas.DataFrame([[1, 2]])).collect() [Row(0=1, 1=2)]
>>> sqlContext.createDataFrame(df.toArrow()).collect() [Row(name='Alice', age=1)] >>> table = pyarrow.table({'0': [1], '1': [2]}) >>> sqlContext.createDataFrame(table).collect() [Row(0=1, 1=2)]
>>> sqlContext.createDataFrame(rdd, "a: string, b: int").collect() [Row(a='Alice', b=1)] >>> rdd = rdd.map(lambda row: row[1]) >>> sqlContext.createDataFrame(rdd, "int").collect() [Row(value=1)] >>> sqlContext.createDataFrame(rdd, "boolean").collect() Traceback (most recent call last): ... pyspark.errors.exceptions.captured.PythonException: ...