2016-09-07 84 views
0
from pyspark.sql.functions import split, explode 

sheshakespeareDF = sqlContext.read.text(fileName).select(removePunctuation(col('value'))) 

shakespeareDF.show(15, truncate=False) 

數據框看起來是這樣的:字數: '列' 對象不是可調用

enter image description here

ss = split(shakespeareDF.sentence," ") 
shakeWordsDFa =explode(ss) 

shakeWordsDF_S=sqlContext.createDataFrame(shakeWordsDFa,'word') 

任何想法,我究竟做錯了什麼?提示說Column is not iterable

我該怎麼辦?我只想將shakeWordsDFa更改爲數據框並重命名。

+1

這是CS110X HW來自edx – eliasah

回答

2

只需使用選擇:

shakespeareDF = sc.parallelize([ 
    ("from fairest creatures we desire increase",), 
    ("that thereby beautys rose might never die",), 
]).toDF(["sentence"]) 

(shakespeareDF 
    .select(explode(split("sentence", " ")).alias("word")) 
    .show(4)) 

## +---------+ 
## |  word| 
## +---------+ 
## |  from| 
## | fairest| 
## |creatures| 
## |  we| 
## +---------+ 
## only showing top 4 rows 

星火SQL列不是數據結構。沒有綁定到數據,只有在特定的DataFrame的環境中進行評估時纔有意義。這種方式Columns表現得更像功能。

+0

很好的解釋 – cdarlint