Nad*_*nes 4

Spark有类似的功能explode(),但并不完全相同。

这是在非常高的水平上爆炸的工作原理。

>>> from pyspark.sql.functions import explode, col

>>> data = {'A': [1, 2]}

>>> df = spark.createDataFrame(data)

>>> df.show()
 +------+
 |     A|
 +------+
 |[1, 2]|
 +------+

>>> df.select(explode(col('A')).alias('normalized')).show()
+----------+
|normalized|
+----------+
|         1|
|         2|
+----------+
Run Code Online (Sandbox Code Playgroud)

另一方面,您可以使用以下方法将 Spark DataFrame 转换为 Pandas DataFrame:

  • spark_df.toPandas() --> 利用 json_normalize() 然后恢复为 Spark DataFrame。

  • 要恢复为 Spark DataFrame,您可以使用spark.createDataFrame(pandas_df).

请注意,这种来回解决方案并不理想,因为调用 toPandas() 会导致将 DataFrame 的所有记录收集 (.collect()) 到驱动程序,并且在处理较大数据集时可能会导致内存错误。

下面的链接提供了有关使用 toPandas() 的更多见解: DF.topandas() throwing error in pyspark

希望这有帮助,祝你好运!