我在sparksession中有一个小的数据框,我想更改列名
my_sess.sql('SELECT * from data LIMIT 10').show()
+---+---+---+---+
| 0| 1| 2| 3|
+---+---+---+---+
|5.1|3.5|1.4|0.2|
|4.9|3.0|1.4|0.2|
|4.7|3.2|1.3|0.2|
|4.6|3.1|1.5|0.2|
|5.0|3.6|1.4|0.2|
|5.4|3.9|1.7|0.4|
|4.6|3.4|1.4|0.3|
|5.0|3.4|1.5|0.2|
|4.4|2.9|1.4|0.2|
|4.9|3.1|1.5|0.1|
+---+---+---+---+
这里所有列名都是数字值,我想将它们更改为a,b,c,d吗?我该怎么办?
my_sess.sql('DESCRIBE data').show()
+--------+---------+-------+
|col_name|data_type|comment|
+--------+---------+-------+
| 0| double| null|
| 1| double| null|
| 2| double| null|
| 3| double| null|
+--------+---------+-------+
我尝试使用ALTER命令,但是没有用。有帮助吗?
答案 0 :(得分:2)
您可以这样重命名列:
new_columns = ["a", "b", "c", "d"]
for old, new in zip(data.columns, new_columns):
data = data.withColumnRenamed(old, new)