【发布时间】:2018-11-16 11:16:16
【问题描述】:
我有一个 pyspark 数据框,例如:
+--------+-------+-------+
| col1 | col2 | col3 |
+--------+-------+-------+
| 25 | 01 | 2 |
| 23 | 12 | 5 |
| 11 | 22 | 8 |
+--------+-------+-------+
我想通过添加这样的新列来创建新的数据框:
+--------------+-------+-------+-------+
| new_column | col1 | col2 | col3 |
+--------------+-------+-------+-------+
| 0 | 01 | 2 | 0 |
| 0 | 12 | 5 | 0 |
| 0 | 22 | 8 | 0 |
+--------------+-------+-------+-------+
我知道我可以通过以下方式添加列:
df.withColumn("new_column", lit(0))
但它最后添加了这样的列:
+--------------+-------+-------+-------------+
| col1 | col1 | col2 | new_column |
+--------------+-------+-------+-------------+
| 25 | 01 | 2 | 0 |
| 23 | 12 | 5 | 0 |
| 11 | 22 | 8 | 0 |
+--------------+-------+-------+-------------+
【问题讨论】:
-
使用 withColumn 和 select('new_column',other columns) 添加。
标签: python apache-spark pyspark apache-spark-sql