问题
is there any generic functions to assign column names in pyspark ?instead of _1,_2,_3....... it has to give col_1,col_2,col_3
+---+---+---+---+---+---+---+---+---+---+---+---+
| _1| _2| _3| _4| _5| _6| _7| _8| _9|_10|_11|_12|
+---+---+---+---+---+---+---+---+---+---+---+---+
| 0| 0| 0| 1| 0| 1| 0| 0| 0| 1| 0| |
| 0| 0| 0| 1| 0| 1| 0| 0| 0| 1| 0| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 0| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 0| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 0| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 0| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 0| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 0| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 0| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 0| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 0| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 0| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 1| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 1| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 1| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 1| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 1| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 1| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 1| |
| 0| 0| 0| 0| 0| 1| 1| 0| 1| 1| 1| |
+---+---+---+---+---+---+---+---+---+---+---+---+
only showing top 20 rows
回答1:
Try this-
df.toDF(*["col_{}".format(i) for i in range(1,len(df.columns)+1)])
来源:https://stackoverflow.com/questions/63259555/is-there-any-generic-functions-to-assign-column-names-in-pyspark