我正在尝试将以下字典:partlistzonesdef(有50个键)转换为数据框:可以说我们有字典:
{1: [60, 127],
2: [21, 43, 61, 19],
3: [186, 154, 37],
4: [99, 68, 80, 87, 128, 98]}
Run Code Online (Sandbox Code Playgroud)
我怎样才能将其转换为这样的数据框:
Index Area
0 1 60
1 1 127
2 2 21
3 2 43
4 2 61
5 2 19
6 3 186
7 3 154
8 3 37
Run Code Online (Sandbox Code Playgroud)
等等?
我正在尝试将 rdd 转换为 Spark 中的数据帧。我的 rdd 是通过整数列表的并行化创建的,在转换为数据帧时我遇到了困难。它返回“TypeError:StructType 无法接受类型为 <class 'int'> 的对象 60651”。
在这里你可以看得更清楚:
# Create a schema for the dataframe
schema = StructType([StructField('zipcd', IntegerType(), True)] )
# Convert list to RDD
rdd = sc.parallelize(zip_cd) #solution: close within []. Another problem for the solution, if I do that, the problem 'lenght does not match: 29275 against 1' arises
#rdd=rdd.map(lambda x:int(x))
# Create data frame
zip_cd1 = spark.createDataFrame(rdd,schema)
#print(zip_cd1.schema)
zip_cd1.show()
Run Code Online (Sandbox Code Playgroud)
它返回给我以下内容:
Py4JJavaError Traceback (most recent call last)
<ipython-input-59-13ef33f842e4> in <module>
9 zip_cd1 …Run Code Online (Sandbox Code Playgroud)