# How to export to dict data port in Pyspark Script

**URL:** <https://forum.knime.com/t/how-to-export-to-dict-data-port-in-pyspark-script/74890>\
**Category:** KNIME Analytics Platform\
**Tags:** python, spark\
**Created:** [October 31, 2023, 11:52am UTC](https://forum.knime.com/t/how-to-export-to-dict-data-port-in-pyspark-script/74890 "2023-10-31T11:52:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JaeHwanChoi](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@JaeHwanChoi](https://forum.knime.com/u/JaeHwanChoi)\
**Post date:** [October 31, 2023, 11:52am UTC](https://forum.knime.com/t/how-to-export-to-dict-data-port-in-pyspark-script/74890/1 "2023-10-31T11:52:36Z")

</div>

Hello KNIME Support Team.

I am using Pyspark Script and want to put all the data from one dictionary in one column. Pyspark Script’s output format must be dataframe so I can output port, but I want to put it in one column and make it dataframe.

I have some code that I have applied in Python, but I am having difficulty applying it to Pyspark.

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/4/f/4f97567288ca430581ab7430de89b25f56c2fddd.png)

How can I put the above values into a single column like below?

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/0/0/00f05d8c35e75718c70e834191a8f3e4b673c43c.png)

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![sascha.wolke](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/s/e0b2c6/32.png) [@sascha.wolke](https://forum.knime.com/u/sascha.wolke)\
**Post date:** [November 16, 2023, 2:47pm UTC](https://forum.knime.com/t/how-to-export-to-dict-data-port-in-pyspark-script/74890/2 "2023-11-16T14:47:49Z")

</div>

Hi @JaeHwanChoi,

You might like to export the details as individual columns using something like this in the [PySpark Script Source](https://kni.me/n/eDKUTMwQxdRp4hek):

```python
models = [
  { 'name': 'model_1', 'indicator_1': 0.65, 'indicator_2': 0.63, 'indicator_3': 0.88 },
  { 'name': 'model_2', 'indicator_1': 0.83, 'indicator_2': 0.76, 'indicator_3': 0.93 }
]
resultDataFrame1 = spark.createDataFrame(models)

```

This results in such a table:

```plaintext
+-----------+-----------+-----------+-------+
|indicator_1|indicator_2|indicator_3|name |
+-----------+-----------+-----------+-------+
|0.65 |0.63 |0.88 |model_1|
|0.83 |0.76 |0.93 |model_2|
+-----------+-----------+-----------+-------+

```

And this schema:

```plaintext
root
 |-- indicator_1: double (nullable = true)
 |-- indicator_2: double (nullable = true)
 |-- indicator_3: double (nullable = true)
 |-- name: string (nullable = true)

```

If you really like to put everything into one column and row, you can do something like this:

```python
models = {
	'model_1': { 'indicator_1': 0.65, 'indicator_2': 0.63, 'indicator_3': 0.88 },
	'model_2': { 'indicator_1': 0.83, 'indicator_2': 0.76, 'indicator_3': 0.93 }
}
resultDataFrame1 = spark.createDataFrame([{ 'dict_detail': models }])

```

This results in such a table:

```plaintext
+--------------------------------------------------------------------------------------------------------------------------------------------------------+
|dict_detail |
+--------------------------------------------------------------------------------------------------------------------------------------------------------+
|{model_2 -> {indicator_1 -> 0.83, indicator_2 -> 0.76, indicator_3 -> 0.93}, model_1 -> {indicator_1 -> 0.65, indicator_2 -> 0.63, indicator_3 -> 0.88}}|
+--------------------------------------------------------------------------------------------------------------------------------------------------------+

```

And this is the schema:

````plaintext
root
 |-- dict_detail: map (nullable = true)
 | |-- key: string
 | |-- value: map (valueContainsNull = true)
 | | |-- key: string
 | | |-- value: double (valueContainsNull = true)```

````

You can find more information about PySpark in the official documentation: [PySpark documentation](https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.SparkSession.createDataFrame.html)

Cheers,  
Sascha

---

<div class="post-metadata">

**Author:** ![JaeHwanChoi](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@JaeHwanChoi](https://forum.knime.com/u/JaeHwanChoi)\
**Post date:** [November 17, 2023, 7:38am UTC](https://forum.knime.com/t/how-to-export-to-dict-data-port-in-pyspark-script/74890/3 "2023-11-17T07:38:03Z")

</div>

Thank you for your response. @sascha.wolke

I’m new to Pyspark and it’s tricky to get it to work with KNIME, so I have one additional question.

When I pull your code into the Pyspark Script in KNIME, the “:” is replaced with “-\>”. Is this an inherent problem?

Thanks.

---

<div class="post-metadata">

**Author:** ![sascha.wolke](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/s/e0b2c6/32.png) [@sascha.wolke](https://forum.knime.com/u/sascha.wolke)\
**Post date:** [November 17, 2023, 9:15am UTC](https://forum.knime.com/t/how-to-export-to-dict-data-port-in-pyspark-script/74890/4 "2023-11-17T09:15:48Z")

</div>

Hi @JaeHwanChoi,

> When I pull your code into the Pyspark Script in KNIME, the “:” is replaced with “-\>”.

I guess you mean the second variant? The single column/row with the dict/struct cannot be used in KNIME, there is no similar type to represent this in KNIME. You might be able to use it in other PySpark nodes, but not in KNIME. The value gets converted into a String in KNIME, if you use the Spark to Table node or the preview, and then contains the arrows in this string. But it is not really usable in KNIME in this way anymore, as it is only a string.

Suggesting going with the first variant, as it output a flat table that can be used directly in KNIME.

Cheers,  
Sascha

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [November 24, 2023, 9:16am UTC](https://forum.knime.com/t/how-to-export-to-dict-data-port-in-pyspark-script/74890/5 "2023-11-24T09:16:30Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
