您能否建議如何計(jì)算以下情況的不同值。我在 PySpark 中有數(shù)據(jù)框(列:'Rank'、'Song'、'Artist'、'Year'、'Lyrics'、'Source')。“歌詞”列包含字符串值,應(yīng)按單詞劃分。我已經(jīng)計(jì)算了“歌詞”列中每一行的所有單詞數(shù)。我還將字符串轉(zhuǎn)換為列表,將結(jié)果保存在新列“uniqWords_count”中。不幸的是,我無法弄清楚如何投入和計(jì)算不同的價(jià)值。如果它可能有用,這是代碼:billdf = billdf.withColumn('allWords_count', f.size(f.split(f.col('Lyrics'), ' ')))billdf = billdf.withColumn('uniqWords_count', f.split(f.col('Lyrics'), ' '))試圖應(yīng)用 countDistinct 函數(shù),但它導(dǎo)致了錯誤:billdf = billdf.withColumn('uniqWords_count', f.countDistinct(f.split(f.col('Lyrics'), ' ')))Py4JJavaError:調(diào)用 o3784.withColumn 時(shí)出錯。:org.apache.spark.sql.AnalysisException:分組表達(dá)式序列為空,并且' Song'不是聚合函數(shù)。如果您不在乎得到哪個(gè)值,則將'(count(DISTINCT split( Lyrics, ' ', -1)) AS uniqWords_count)' 包裝在窗口函數(shù)中或?qū)?' ' 包裝在 first() (或 first_value)中。;;Song
1 回答

DIEA
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊
Mohammad Murtaza Hashmi提出了一個(gè)解決方案。就我而言,它看起來像這樣:
billdf = billdf.withColumn('uniqWords',f.size(f.array_distinct("uniqWords")))
非常感謝您的幫助!
添加回答
舉報(bào)
0/150
提交
取消