Pyspark如何从数据框架中创建自定义的CSV

发布于 2025-01-21 21:27:14 字数 1163 浏览 2 评论 0原文

我有以下数据框架,我需要将其加载到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文