繁体   English   中英

将熊猫数据框转换为Spark数据框时出错

[英]Error while converting a pandas dataframe to spark Dataframe

我的熊猫数据框

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 Dataframe sdf2时,出现以下错误。 我尝试了不同的数据类型,但是徒劳。

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

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

那是因为数组中有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

尝试改用Python list

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM