gpt4 book ai didi

python - 属性错误 : 'DataFrame' object has no attribute 'map'

转载 作者:IT老高 更新时间:2023-10-28 22:06:38 24 4
gpt4 key购买 nike

我想使用下面的代码转换 Spark 数据框来添加:

from pyspark.mllib.clustering import KMeans
spark_df = sqlContext.createDataFrame(pandas_df)
rdd = spark_df.map(lambda data: Vectors.dense([float(c) for c in data]))
model = KMeans.train(rdd, 2, maxIterations=10, runs=30, initializationMode="random")

详细的错误信息是:

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-a19a1763d3ac> in <module>()
1 from pyspark.mllib.clustering import KMeans
2 spark_df = sqlContext.createDataFrame(pandas_df)
----> 3 rdd = spark_df.map(lambda data: Vectors.dense([float(c) for c in data]))
4 model = KMeans.train(rdd, 2, maxIterations=10, runs=30, initializationMode="random")

/home/edamame/spark/spark-2.0.0-bin-hadoop2.6/python/pyspark/sql/dataframe.pyc in __getattr__(self, name)
842 if name not in self.columns:
843 raise AttributeError(
--> 844 "'%s' object has no attribute '%s'" % (self.__class__.__name__, name))
845 jc = self._jdf.apply(name)
846 return Column(jc)

AttributeError: 'DataFrame' object has no attribute 'map'

有谁知道我在这里做错了什么?谢谢!

最佳答案

您不能 map 数据帧,但您可以将数据帧转换为 RDD 并通过执行 spark_df.rdd.map() 进行映射。在 Spark 2.0 之前,spark_df.map 将别名为 spark_df.rdd.map()。在 Spark 2.0 中,您必须首先显式调用 .rdd

关于python - 属性错误 : 'DataFrame' object has no attribute 'map' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39535447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com