如何将dict转换为spark map输出
16lz
2021-01-22
I'm working with spark and python. I would like to transform my input dataset.
我正在使用spark和python。我想转换我的输入数据集。
My input dataset (RDD)
我的输入数据集(RDD)
-------------------------------------------------------------
| id | var |
-------------------------------------------------------------
| 1 |"[{index: 1, value: 200}, {index: 2, value: A}, ...]" |
| 2 |"[{index: 1, value: 140}, {index: 2, value: C}, ...]" |
| .. | ... |
-------------------------------------------------------------
I would like to have this DataFrame (output dataset)
我想有这个DataFrame(输出数据集)
----------------------
| id | index | value |
----------------------
| 1 | 1 | 200 |
| 1 | 2 | A |
| 1 | ... | ... |
| 2 | 1 | 140 |
| 2 | 2 | C |
| ...| ... | ... |
----------------------
I create a map function
我创建了一个地图功能
def process(row):
my_dict = {}
for item in row['value']:
my_dict['id'] = row['id']
my_dict['index'] = item['index']
my_dict['value'] = item['value']
return my_dict
I would like to map my process function like this:
我想像我这样映射我的过程函数:
output_rdd = input_rdd.map(process)
Is it possible to do this on this way (or a simpler way)?
是否有可能以这种方式(或更简单的方式)这样做?
1 个解决方案
#1
0
I found the solution:
我找到了解决方案:
output_rdd = input_rdd.map(lambda row:process(row)).flatMap(lambda x: x)
更多相关文章
- 检查 NaN 数据值 (C/C++/Python 实现)
- 【python网络爬虫三】爬取动态数据及数据入库
- 数据挖掘(三)分类模型的描述与性能评估,以决策树为例
- 用于Python项目的低内存和最快查询数据库
- python爬虫:爬取豌豆荚APP第一页数据信息(selenium)
- Pandas 文本数据方法 findall( )
- TensorFlow数据集(一)——数据集的基本使用方法
- python常用数据类型-字典
- python脚本下载并解码MNIST数据遇到的问题