Pyspark如何从数据框架中创建自定义的CSV
我有以下数据框架,我需要将其加载到CSV中,并具有自定义的行,并且
common_df.show()
+--------+----------+-----+----+-----+-----------+-------+---+
|name |department|state|id |name | department| state | id|
+--------+----------+-----+----+-----+-----------+-------+---+
|James |Sales |NY |101 |James| Sales1 |null |101|
|Maria |Finance |CA |102 |Maria| Finance | |102|
|Jen |Marketing |NY |103 |Jen | |NY2 |103|
我遵循以下方法,目前将DF转换为CSV,
pandasdf=common_df.toPandas()
pandasdf.to_csv("s3://mylocation/result.csv")
以上将在CSV中使用相同的结构转换。 但是,我需要从上面的格式构造到下面的内容,我认为解决方案是将每一行分为两个分配数据框内的ID。但是我没有直接从火花
|name |dept |state|id |
------------------------------------
101 |James |Sales |NY |101 |
|James |null |NY |101 |
------------------------------------
102 |Maria |Finance | |102 |
|Maria |Finance |CA |102 |
-------------------------------------
103 |Jen |Marketing |NY |103 |
|Jen | |NY2 |103 |
------------------------------------
解决任何解决方案中看到任何例子或解决方案?
i have below data frame which i need to load in to csv with customized row and values
common_df.show()
+--------+----------+-----+----+-----+-----------+-------+---+
|name |department|state|id |name | department| state | id|
+--------+----------+-----+----+-----+-----------+-------+---+
|James |Sales |NY |101 |James| Sales1 |null |101|
|Maria |Finance |CA |102 |Maria| Finance | |102|
|Jen |Marketing |NY |103 |Jen | |NY2 |103|
i am following below approach currently to convert df to csv
pandasdf=common_df.toPandas()
pandasdf.to_csv("s3://mylocation/result.csv")
The above going to convert with same structure in csv.
however i need to structure from above format to something below, I think the solution would be to split each row to two allocating the id on left within data frame. but i don't see any example or solution directly from spark
|name |dept |state|id |
------------------------------------
101 |James |Sales |NY |101 |
|James |null |NY |101 |
------------------------------------
102 |Maria |Finance | |102 |
|Maria |Finance |CA |102 |
-------------------------------------
103 |Jen |Marketing |NY |103 |
|Jen | |NY2 |103 |
------------------------------------
Any solution to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论