pyspark : 将 DataFrame 转换为 RDD[string] [英] pyspark : Convert DataFrame to RDD[string]

查看:51
本文介绍了pyspark : 将 DataFrame 转换为 RDD[string]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 pyspark.sql.dataframe.DataFrame 转换为 pyspark.rdd.RDD[String]

我将 DataFrame df 转换为 RDD data:

I converted a DataFrame df to RDD data:

data = df.rdd
type (data)
## pyspark.rdd.RDD 

新的RDD data 包含Row

first = data.first()
type(first)
## pyspark.sql.types.Row

data.first()
Row(_c0=u'aaa', _c1=u'bbb', _c2=u'ccc', _c3=u'ddd')

我想将 Row 转换为 String 的列表,如下例所示:

I'd like to convert Row to list of String , like example below:

u'aaa',u'bbb',u'ccc',u'ddd'

谢谢

推荐答案

PySpark Row 只是一个 tuple 并且可以这样使用.这里你需要的只是一个简单的 map(或者 flatMap,如果你还想把行展平)和 list:

PySpark Row is just a tuple and can be used as such. All you need here is a simple map (or flatMap if you want to flatten the rows as well) with list:

data.map(list)

或者如果您期望不同的类型:

or if you expect different types:

data.map(lambda row: [str(c) for c in row])

这篇关于pyspark : 将 DataFrame 转换为 RDD[string]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆