How to add a new column in the first ordinal position in a pyspark dataframe

We have a pyspark data frame as given here:
+------+------+-------+
|col1 | col2 | col3 |
+------+------+-------+
| 21 | 06 | 2 |
| 25 | 13 | 5 |
| 17 | 23 | 8 |
+------+------+-------+
We want to create new dataframe by adding a new column as first cloumn like given here under:
+---------+-------+-------+-------+
| new_col | col1 | col2 | col3 |
+---------+-------+-------+-------+
| 0 | 21 | 06 | 4 |
| 0 | 25 | 13 | 6 |
| 0 | 17 | 23 | 8 |
+---------+-------+-------+-------+
We are adding cloumn by:

df.withColumn("new_col", lit(0))
but it adding column in last:

+------+-------+-------+--------+
| col1 | col1 | col2 | new_col|
+------+-------+-------+--------+
| 21 | 01 | 2 | 0 |
| 25 | 12 | 5 | 0 |
| 17 | 22 | 8 | 0 |
+------+-------+-------+--------+

Please help us to get desired result.
Thanks in advance.

Tagged:
Sign In or Register to comment.