Is there any generic functions to assign column names in pyspark?

人走茶凉 提交于 2020-08-10 22:54:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!