简体   繁体   中英

How to create Spark Row from list of key-value pairs

Suppose I have got a list of key-value pairs:

kvs = [('x', 0), ('a', 1)]

Now I'd like to create a Spark Row from kvs with the same order of keys as in kvs .
How to do it in Python ?

I haven't run it yet but may you check once I will edit after running if fails.

from pyspark.sql import Row
kvs = [('x', 0), ('a', 1)]
h = {}
[h.update({k:v}) for k,v in kvs]
row = Row(**h)

You can:

from pyspark.sql import Row

Row(*[k for k, _ in kvs])(*[v for _, v in kvs])

but in my opinion it is better to avoid Row whatsoever. Other than being a convenient class to represent local values fetched from the JVM backend, it has no special meaning in Spark. In almost every context:

tuple(v for _, v in kvs)

is perfectly valid replacement for Row .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM