我有行元组格式的数据 -
Row(Sentence=u'When, for the first time I realized the meaning of death.')
我想像这样将其转换为字符串格式 -
(u'When, for the first time I realized the meaning of death.')
我尝试过这样(假设"a"在行图普尔中有数据(-
b = sc.parallelize(a)
b = b.map(lambda line: tuple([str(x) for x in line]))
print(b.take(4))
但我得到的结果是这样的——
[('W', 'h', 'e', 'n', ',', ' ', 'f', 'o', 'r', ' ', 't', 'h', 'e', ' ', 'f', 'i', 'r', 's', 't', ' ', 't', 'i', 'm', 'e', ' ', 'I', ' ', 'r', 'e', 'a', 'l', 'i', 'z', 'e', 'd', ' ', 't', 'h', 'e', ' ', 'm', 'e', 'a', 'n', 'i', 'n', 'g', ' ', 'o', 'f', ' ', 'd', 'e', 'a', 't', 'h', '.')]
有人知道我在这里做错了什么吗?
下面是代码:
col = 'your_column_name'
val = df.select(col).collect()
val2 = [ ele.__getattr__(col) for ele in val]
对于单个Row
(你为什么甚至...(,它应该是:
a = Row(Sentence=u'When, for the first time I realized the meaning of death.')
b = sc.parallelize([a])
并扁平化
b.map(lambda x: x.Sentence)
或
b.flatMap(lambda x: x)
尽管sc.parallelize(a)
已经是您需要的格式 - 因为您传递了Iterable
,Spark 将Row
遍历所有字段以创建RDD
下面对我有用 list_val = df.selectExpr("max(Location) as loc").collect() str_val = [e['loc'] for e in list_val][0]