【发布时间】:2021-02-08 10:01:10
【问题描述】:
我正在尝试从 pandas 数据帧创建一个 pyspark 数据帧。
import pandas as pd
from pyspark.sql.types import StructType, StructField, IntegerType, DoubleType
a_dict = {0: [(0, 9.821), (1, 82.185)]}
a_pd = pd.DataFrame.from_dict(a_dict.items())
a_pd.columns = ["row_num", "val"]
a_str = StructType([StructField("id", IntegerType(), True), StructField("prob", DoubleType(), True)])
my_schema = StructType([ StructField("row_num", LongType(), True),StructField("val", list(a_str), True)]) # error
a_df = spark.createDataFrame(a_pd, schema=my_schema)
错误:
AssertionError: dataType [StructField(id,IntegerType,true), StructField(prob,DoubleType,true)] should be an instance of <class 'pyspark.sql.types.DataType'>
如何定义一个有效的架构
list of tuple of (int, DoubleType)
这样pyspark也能理解?
谢谢
【问题讨论】:
标签: pandas dataframe apache-spark pyspark