You can filter the rows with idx != 1
, and add a row using union
:
from pyspark.sql import functions as F, Window
L1 = ['na',5.6,2.4]
data = [('fr',8.8,6.6),
('nr',4.4,2.5),
('cc',2.3,3.9)]
df = spark.createDataFrame(data, ['loc', 'col', 'io'])
df2 = df.withColumn(
"idx",
F.row_number().over(Window.orderBy('loc'))
).filter('idx != 1').union(spark.createDataFrame([L1 + [1]]))
df2.show()
+---+---+---+---+
|loc|col| io|idx|
+---+---+---+---+
| fr|8.8|6.6| 2|
| nr|4.4|2.5| 3|
| na|5.6|2.4| 1|
+---+---+---+---+
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…