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

时间:2018-06-19 15:43:57

标签: python pandas apache-spark pyspark

我的熊猫数据框

echo "THIS IS TEST FILE." > /tmp/work/abc.txt  ## at 01:00 Hrs
echo "THIS IS TEST FILE." > /tmp/work/def.txt  ## at 06:00 Hrs

每个单元格都是一个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, ...

在创建spark数据框sdf2时,出现以下错误。我尝试了不同的数据类型,但徒劳无功。

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

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

1 个答案:

答案 0 :(得分:0)

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

相关问题