79627361

Date: 2025-05-18 10:41:36
Score: 1.5
Natty:
Report link

You can reorder the columns by using the select() function in PySpark. In the select function, you can pass the column name as a string or multiple columns as a list.

Please find the reference in the pyspark documentation

data = [("Alice", 25, "Engineer"), ("Bob", 30, "Doctor"), ("Charlie", 28, "Teacher")]
columns = ["name", "age", "occupation"]
df = spark.createDataFrame(data, columns)

new_column_order = ["occupation", "name", "age"]
reordered_df = df.select(new_column_order)

reordered_df.show()

# +----------+-------+---+
# |occupation| name|age|
# +----------+-------+---+
# | Engineer| Alice| 25|
# | Doctor| Bob| 30|
# | Teacher|Charlie| 28|
# +----------+-------+---+

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Filler text (0.5): ----------
  • Filler text (0): ----------
  • Filler text (0): ----------
  • Low reputation (1):
Posted by: Shivansh Keshari