将Pyspark DataFrame 转换成Python字典列表

8

嗨,我是新手Pyspark用户,想将Pyspark.sql.dataframe转换成字典列表。

以下是我的数据框,类型为<class '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}]

我应该怎样做?先感谢您!

1个回答

9
您可以将每一行映射为一个字典并收集结果: df.rdd.map(lambda row: row.asDict()).collect()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接