Adding a Vectors Column to a pyspark DataFrame

孤街醉人 提交于 2019-12-08 06:35:58

问题


How do I add a Vectors.dense column to a pyspark dataframe?

import pandas as pd
from pyspark import SparkContext
from pyspark.sql import SQLContext
from pyspark.ml.linalg import DenseVector

py_df = pd.DataFrame.from_dict({"time": [59., 115., 156., 421.], "event": [1, 1, 1, 0]})

sc = SparkContext(master="local")
sqlCtx = SQLContext(sc)
sdf = sqlCtx.createDataFrame(py_df)
sdf.withColumn("features", DenseVector(1))

Gives an error in file anaconda3/lib/python3.6/site-packages/pyspark/sql/dataframe.py, line 1848:

AssertionError: col should be Column

It doesn't like the DenseVector type as a column. Essentially, I have a pandas dataframe that I'd like to transform to a pyspark dataframe and add a column of the type Vectors.dense. Is there another way of doing this?


回答1:


Constant Vectors cannot be added as literal. You have to use udf:

from pyspark.sql.functions import udf
from pyspark.ml.linalg import VectorUDT

one = udf(lambda: DenseVector([1]), VectorUDT())
sdf.withColumn("features", one()).show()

But I am not sure why you need that at all. If you want to transform existing columns into Vectors use appropriate pyspark.ml tools, like VectorAssembler - Encode and assemble multiple features in PySpark

from pyspark.ml.feature import VectorAssembler

VectorAssembler(inputCols=["time"], outputCol="features").transform(sdf)


来源:https://stackoverflow.com/questions/49832877/adding-a-vectors-column-to-a-pyspark-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!