将数据帧转换为spark数据帧时出错

2024-09-30 01:36:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我的熊猫数据帧

df4.head()
                     features
 0          [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, ...
 1          [0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, ...

每个单元格都是一个python列表。你知道吗

mySchema=StructType([StructField("features",ArrayType(IntegerType()),True)])
sdf2=sqlCtx.createDataFrame(df4,schema=mySchema)

在创建spark数据帧sdf2时,出现以下错误。我尝试了不同的数据类型,但没有成功。你知道吗

Error: element in array field features: IntegerType can not accept object 0 in type <class 'numpy.int64'>

我想在Pysark中运行BucketedRandomProjectionLSH,它接受带有数据向量的单个列。你知道吗


Tags: 数据intrue列表headfeaturesdf4structfield
1条回答
网友
1楼 · 发布于 2024-09-30 01:36:12

这是因为数组中有numpy.int64对象。你知道吗

Spark不接受这一点。你知道吗

df = pd.DataFrame([
    (np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]),),
    (np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]),),
], columns = ['features'])

type(df.iloc[0]['features'][0])
> numpy.int64

df = pd.DataFrame([
    ([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],),
    ([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],),
], columns = ['features'])

type(df.iloc[0]['features'][0])
> int

尝试改用Pythonlist。你知道吗

相关问题 更多 >

    热门问题