我有一个 pyspark 数据框,如:
+--------+-------+-------+
| col1 | col2 | col3 |
+--------+-------+-------+
| 25 | 01 | 2 |
| 23 | 12 | 5 |
| 11 | 22 | 8 |
+--------+-------+-------+
Run Code Online (Sandbox Code Playgroud)
我想通过添加这样的新列来创建新的数据框:
+--------------+-------+-------+-------+
| new_column | col1 | col2 | col3 |
+--------------+-------+-------+-------+
| 0 | 01 | 2 | 0 |
| 0 | 12 | 5 | 0 |
| 0 | 22 | 8 | 0 |
+--------------+-------+-------+-------+
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过以下方式添加列:
df.withColumn("new_column", lit(0))
Run Code Online (Sandbox Code Playgroud)
但它最后像这样添加了列:
+--------------+-------+-------+-------------+
| …Run Code Online (Sandbox Code Playgroud)