简体   繁体   English

PySpark - 从字典中创建一个 Dataframe,其中包含每个键的值列表

[英]PySpark - Create a Dataframe from a dictionary with list of values for each key

I've this type of dictionary:我有这种类型的字典:

{'xy': [['value1', 'value2'], ['value3', 'value4']],
 'yx': [['value5', 'value6'], ['value7', 'value8']]}

I would like to create a dataFrame pyspark in which I have 3 columns and 2 rows.我想创建一个 dataFrame pyspark ,其中我有 3 列和 2 行。 Every key of the dict has a row. dict 的每个键都有一行。 For example, first row:例如,第一行:

First column: xy
Second column: ["value1", "value2"]
Third column: ["value3", "value4"]

 

What's the better way to do this?有什么更好的方法来做到这一点? I'm only able to create 2 columns, in which there is a key and only one column with all the list but it's not my desired result.我只能创建 2 列,其中有一个键,并且只有一列包含所有列表,但这不是我想要的结果。

This is your data dictionary:这是您的数据字典:

data = {
    'xy': [['value1', 'value2'], ['value3', 'value4']],
    'yx': [['value5', 'value6'], ['value7', 'value8']]
}

You can just use a for loop:您可以只使用 for 循环:

df = spark.createDataFrame(
    [[k] + v for k, v in data.items()],
    schema=['col1', 'col2', 'col3']
)

df.show(10, False)
+----+----------------+----------------+
|col1|col2            |col3            |
+----+----------------+----------------+
|xy  |[value1, value2]|[value3, value4]|
|yx  |[value5, value6]|[value7, value8]|
+----+----------------+----------------+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将嵌套字典键值转换为 pyspark dataframe - Transform nested dictionary key values to pyspark dataframe 从字典值列表创建 dataframe - Create dataframe from list of dictionary values 有没有一种方法可以从带有值列表的字典中创建数据框? - Is there a way to create a dataframe from a dictionary with list of values? 从多个字典值列表创建 dataframe - Create a dataframe from multiple list of dictionary values Python:使用[0] =键和[1:] =值从列表创建字典 - Python: Create Dictionary From List with [0] = Key and [1:]= Values 从多个列表创建字典,其中每个列表没有一个键的值,但每个列表都有所有键的值 - create dictionary from multiple lists, where each list has not the values for one key, but each list has values for all keys 从具有分配给每个键的多个值的字典创建数据帧 - Creating Dataframe from dictionary that has multiple values assigned to each key 如何创建一个字典,从列表中分配值并为每个值生成相同的键 - How to create a dictionary assigning values from a list and generate the same key for each one 从包含每个键的多个uniqe值的字符串列表创建3级字典 - Create a 3 level dictionary from a list of strings with multiple uniqe values for each key 如何从列表中创建字典,其中每个元素的计数是键,值是相应元素的列表? - How to create a dictionary from a list where the count of each element is the key and values are the lists of the according element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM