我得到了以下数据帧:
+--------+---------------+--------------------+---------+
|province| city| infection_case|confirmed|
+--------+---------------+--------------------+---------+
| Seoul| Yongsan-gu| Itaewon Clubs| 139|
| Seoul| Gwanak-gu| Richway| 119|
| Seoul| Guro-gu| Guro-gu Call Center| 95|
| Seoul| Yangcheon-gu|Yangcheon Table T...| 43|
| Seoul| Dobong-gu| Day Care Center| 43|
现在我想根据CSV文件更改列名(第一行),如下所示:
province,any_other__name
city,any_other__name
infection_case,any_other__name
confirmed,any_other__name
这是我的代码:
cases = spark.read.load("/home/tool/Desktop/database/TEST/archive/Case.csv",format="csv", sep=",", inferSchema="true", header="true")
cases = cases.select('province','city','infection_case','confirmed')
cases \
.write \
.mode('overwrite') \
.option('header', 'true') \
.csv('8.csv')
最好的解决方案是使用^{} 方法
这里的解决方案 pyspark中使用selectExpr()重命名使用“as”关键字将列“Old\u name”重命名为“New\u name”
相关问题 更多 >
编程相关推荐