首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >手动创建

手动创建
EN

Stack Overflow用户
提问于 2019-09-16 15:11:18
回答 6查看 114.7K关注 0票数 38

我正在尝试手动创建一个具有特定数据的:

代码语言:javascript
复制
row_in = [(1566429545575348), (40.353977), (-111.701859)]
rdd = sc.parallelize(row_in)
schema = StructType(
    [
        StructField("time_epocs", DecimalType(), True),
        StructField("lat", DecimalType(), True),
        StructField("long", DecimalType(), True),
    ]
)
df_in_test = spark.createDataFrame(rdd, schema)

当我试图显示dataframe时,这会产生一个错误,所以我不知道如何做到这一点。

然而,火花文档似乎有点令我费解,当我试图遵循这些指令时,我也遇到了类似的错误。

有人知道怎么做吗?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2019-09-16 15:40:31

简单的数据创建:

代码语言:javascript
复制
df = spark.createDataFrame(
    [
        (1, "foo"),  # create your data here, be consistent in the types.
        (2, "bar"),
    ],
    ["id", "label"]  # add your column names here
)

df.printSchema()
root
 |-- id: long (nullable = true)
 |-- label: string (nullable = true)

df.show()
+---+-----+                                                                     
| id|label|
+---+-----+
|  1|  foo|
|  2|  bar|
+---+-----+

根据官方医生

  • 当架构是列名列表时,将从数据推断每个列的类型。(↑上面的例子)
  • 当模式是pyspark.sql.types.DataType或数据类型字符串时,它必须与实际数据匹配。(↓下面的例子)
代码语言:javascript
复制
# Example with a datatype string
df = spark.createDataFrame(
    [
        (1, "foo"),  # Add your data here
        (2, "bar"),
    ],  
    "id int, label string",  # add column names and types here
)

# Example with pyspark.sql.types
from pyspark.sql import types as T
df = spark.createDataFrame(
    [
        (1, "foo"),  # Add your data here
        (2, "bar"),
    ],
    T.StructType(  # Define the whole schema within a StructType
        [
            T.StructField("id", T.IntegerType(), True),
            T.StructField("label", T.StringType(), True),
        ]
    ),
)


df.printSchema()
root
 |-- id: integer (nullable = true)  # type is forced to Int
 |-- label: string (nullable = true)

此外,您还可以从Pandas dataframe创建数据create,模式将从Pandas dataframe的类型中推断:

代码语言:javascript
复制
import pandas as pd
import numpy as np


pdf = pd.DataFrame(
    {
        "col1": [np.random.randint(10) for x in range(10)],
        "col2": [np.random.randint(100) for x in range(10)],
    }
)


df = spark.createDataFrame(pdf)

df.show()
+----+----+
|col1|col2|
+----+----+
|   6|   4|
|   1|  39|
|   7|   4|
|   7|  95|
|   6|   3|
|   7|  28|
|   2|  26|
|   0|   4|
|   4|  32|
+----+----+
票数 87
EN

Stack Overflow用户

发布于 2019-09-20 18:00:03

以@Steven的回答为基础:

代码语言:javascript
复制
field = [
    StructField("MULTIPLIER", FloatType(), True),
    StructField("DESCRIPTION", StringType(), True),
]
schema = StructType(field)
multiplier_df = sqlContext.createDataFrame(sc.emptyRDD(), schema)

将创建一个空白数据格式。

我们现在可以简单地向它添加一行:

代码语言:javascript
复制
l = [(2.3, "this is a sample description")]
rdd = sc.parallelize(l)
multiplier_df_temp = spark.createDataFrame(rdd, schema)
multiplier_df = wtp_multiplier_df.union(wtp_multiplier_df_temp)
票数 6
EN

Stack Overflow用户

发布于 2021-02-24 03:40:18

这个答案演示了如何使用PySpark、create_dftoDF创建一个createDataFrame DataFrame。

代码语言:javascript
复制
df = spark.createDataFrame([("joe", 34), ("luisa", 22)], ["first_name", "age"])

df.show()
代码语言:javascript
复制
+----------+---+
|first_name|age|
+----------+---+
|       joe| 34|
|     luisa| 22|
+----------+---+

您还可以传递给createDataFrame一个RDD和模式,以更精确地构造DataFrames:

代码语言:javascript
复制
from pyspark.sql import Row
from pyspark.sql.types import *

rdd = spark.sparkContext.parallelize([
    Row(name='Allie', age=2),
    Row(name='Sara', age=33),
    Row(name='Grace', age=31)])

schema = schema = StructType([
   StructField("name", StringType(), True),
   StructField("age", IntegerType(), False)])

df = spark.createDataFrame(rdd, schema)

df.show()
代码语言:javascript
复制
+-----+---+
| name|age|
+-----+---+
|Allie|  2|
| Sara| 33|
|Grace| 31|
+-----+---+

来自我的create_df项目的奎因允许两者兼而有之--它简洁且全面描述:

代码语言:javascript
复制
from pyspark.sql.types import *
from quinn.extensions import *

df = spark.create_df(
    [("jose", "a"), ("li", "b"), ("sam", "c")],
    [("name", StringType(), True), ("blah", StringType(), True)]
)

df.show()
代码语言:javascript
复制
+----+----+
|name|blah|
+----+----+
|jose|   a|
|  li|   b|
| sam|   c|
+----+----+

与其他方法相比,toDF没有任何优势:

代码语言:javascript
复制
from pyspark.sql import Row

rdd = spark.sparkContext.parallelize([
    Row(name='Allie', age=2),
    Row(name='Sara', age=33),
    Row(name='Grace', age=31)])
df = rdd.toDF()
df.show()
代码语言:javascript
复制
+-----+---+
| name|age|
+-----+---+
|Allie|  2|
| Sara| 33|
|Grace| 31|
+-----+---+
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57959759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档