Spark(Pyspark)如何将Dataframe字符串列转换为Dataframe多列

2024-10-02 06:29:17 发布

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

我有一个低于pyspark的DataFrame[recoms: string],其中recoms列的值是字符串类型。每一行在这里代表一个字符串。你知道吗

+--------------------+
|         recoms     |
+--------------------+
|{"a":"1","b":"5",..}|
|{"a":"2","b":"4",..}|
|{"a":"3","b":"9",..}|
+--------------------+

上面的行没有定义的模式来使用from\u json方法。所以呢我在找其他的选择。如何使用pysparksql函数转换或拆分为多值dataframe列,如下所示。在上表中,:左边的所有值都是dataframe列名,:右边的所有值都是行值。你知道吗

+--------+---+
|    a   | b |
+--------+---+
|    1   |  5|
|    2   |  4|
|    3   |  9|
+--------+---+

我试过分解sql函数 df.select(explode("recoms")).show()并得到以下错误。你知道吗

org.apache.spark.sql.AnalysisException: cannot resolve 'explode(true_recoms)' due to data type mismatch: input to function explode should be array or map type, not string;;


Tags: to函数字符串类型dataframesqlstring定义

热门问题