我读过类似的问题,但没有找到解决我的具体问题的方法。
我有一个列表
l = [1, 2, 3]
以及一个数据框
df = sc.parallelize([
['p1', 'a'],
['p2', 'b'],
['p3', 'c'],
]).toDF(('product', 'name'))
我想获取一个新的DataFrame,其中列表l
被添加为另一列,即
+-------+----+---------+
|product|name| new_col |
+-------+----+---------+
| p1| a| 1 |
| p2| b| 2 |
| p3| c| 3 |
+-------+----+---------+
使用JOIN的方法,我正在将df连接到一个
sc.parallelize([[1], [2], [3]])
执行失败了。使用withColumn
方法的尝试,例如:
new_df = df.withColumn('new_col', l)
由于列表不是Column
对象,因此失败。