简体   繁体   English

如何将一列中的字典列表拆分为 pyspark dataframe 中的两列?

[英]How to split list of dictionary in one column into two columns in pyspark dataframe?

在此处输入图像描述 I want to split the filteredaddress column of the spark dataframe above into two new columns that are Flag and Address:我想将上面的 spark dataframe 的过滤地址列拆分为两个新列,即标志和地址:

customer_id|pincode|filteredaddress|                                                              Flag| Address
1000045801 |121005 |[{'flag':'0', 'address':'House number 172, Parvatiya Colony Part-2 , N.I.T'}]
1000045801 |121005 |[{'flag':'1', 'address':'House number 172, Parvatiya Colony Part-2 , N.I.T'}]
1000045801 |121005 |[{'flag':'1', 'address':'House number 172, Parvatiya Colony Part-2 , N.I.T'}]

Can anyone please tell me how can I do it?谁能告诉我我该怎么做?

You can get the values from filteredaddress map column using the keys:您可以使用以下键从filteredaddress地址 map 列中获取值:

df2 = df.selectExpr(
    'customer_id', 'pincode',
    "filteredaddress['flag'] as flag", "filteredaddress['address'] as address"
)

Other ways to access map values are:访问 map 值的其他方法是:

import pyspark.sql.functions as F

df.select(
    'customer_id', 'pincode',
    F.col('filteredaddress')['flag'],
    F.col('filteredaddress')['address']
)

# or, more simply

df.select(
    'customer_id', 'pincode',
    'filteredaddress.flag',
    'filteredaddress.address'
)

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

相关问题 如何在 pyspark 中创建具有两个 dataframe 列的字典? - How to create a dictionary with two dataframe columns in pyspark? 如何将数据框的一列中的值列表均等地分为多个列 - how to split a list of values in one column of a dataframe into various columns equally 将 pyspark 数据框中的两列转换为一个 python 字典 - Convert two columns in pyspark dataframe into one python dictionary 有没有办法将具有列表值的两列组合成一列,其中包含 pyspark dataframe 的列表值 - Is there a way to combine two columns having list values into one column with list value for a pyspark dataframe 如何将字符串从一列拆分为与列表匹配的两列? - How to split string from one column into two columns that match with the list? 如何在数据框熊猫中将一列分为两列? - How to split a column into two columns in dataframe pandas? 如何将 dataframe 字符串列拆分为两列? - How to split a dataframe string column into two columns? 将字典列拆分为单独的列,融化数据框 - Split list of dictionary column to separate columns, melting the dataframe PYTHON DATAFRAME-将数字[0,0] DATAFRAME的一个列拆分为两个列 - PYTHON DATAFRAME - SPLIT ONE COLUMN of numbers [0,0] DATAFRAME into TWO COLUMNS 如何将 dataframe 列拆分为多列 - How to split one dataframe column into many columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM