将pyspark dataframe转换为python字典列表

2024-06-16 11:12:15 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我是pyspark的新手,我正在尝试将pyspark.sql.dataframe转换为字典列表

下面是我的数据帧,类型为<;类“pyspark.sql.dataframe.dataframe”>;:

+------------------+----------+------------------------+
|             title|imdb_score|Worldwide_Gross(dollars)|
+------------------+----------+------------------------+
| The Eight Hundred|       7.2|               460699653|
| Bad Boys for Life|       6.6|               426505244|
|             Tenet|       7.8|               334000000|
|Sonic the Hedgehog|       6.5|               308439401|
|          Dolittle|       5.6|               245229088|
+------------------+----------+------------------------+

我想把它转换成:

[{"title":"The Eight Hundred", "imdb_score":7.2, "Worldwide_Gross(dollars)":460699653},
 {"title":"Bad Boys for Life", "imdb_score":6.6, "Worldwide_Gross(dollars)":426505244},
 {"title":"Tenet", "imdb_score":7.8, "Worldwide_Gross(dollars)":334000000},
 {"title":"Sonic the Hedgehog", "imdb_score":6.5, "Worldwide_Gross(dollars)":308439401},
 {"title":"Dolittle", "imdb_score":5.6, "Worldwide_Gross(dollars)":245229088}]

我该怎么做?提前谢谢


Tags: thedataframeforsqltitleworldwidepysparkimdb