python - 无法在 PySpark 中创建数据框
问题描述
我想使用以下代码在 PySpark 中创建一个数据框
from pyspark.sql import *
from pyspark.sql.types import *
temp = Row("DESC", "ID")
temp1 = temp('Description1323', 123)
print temp1
schema = StructType([StructField("DESC", StringType(), False),
StructField("ID", IntegerType(), False)])
df = spark.createDataFrame(temp1, schema)
但我收到以下错误:
TypeError:StructType 不能接受类型“str”中的对象“Description1323”
我的代码有什么问题?
解决方案
问题是你正在传递一个Row
你应该传递一个Row
s列表的地方。尝试这个:
from pyspark.sql import *
from pyspark.sql.types import *
temp = Row("DESC", "ID")
temp1 = temp('Description1323', 123)
print temp1
schema = StructType([StructField("DESC", StringType(), False),
StructField("ID", IntegerType(), False)])
df = spark.createDataFrame([temp1], schema)
df.show()
结果:
+---------------+---+
| DESC| ID|
+---------------+---+
|Description1323|123|
+---------------+---+
推荐阅读
- java - 使用 Picasso 从 Firebase 获取图像问题
- sql-server - 如何将 Docker 与 Azure Data Studio 连接起来?
- javascript - Flipkart 如何访问 dom 元素以进行集成测试?
- tsql - MS-SQL:重建与重新创建的空间开销
- c# - 如何避免 EF Core 中的不安全上下文操作?
- bash - 从 netstat 端口检索路径的单线器
- istio - Istio 注入 DB 应用,将其服务类型设为 NodePort,该节点端口无法访问
- excel - VBA 刷新数据透视表
- amazon-web-services - 使用 terraform 创建 aws_flow_log 时出现访问错误
- spring-boot - Rest api 任务的黑客等级类添加