我的火花資料框如下:
target_id other_ids
3733345 [3731634, 3729995, 3728014, 3708332, 3720...
3725312 [3711541, 3726052, 3733763, 900056057, 371...
3717114 [3701718, 3713481, 3715433, 3714825, 3731...
3408996 [3405896, 3250400, 3237054, 3242492, 3256...
3354970 [3354969, 3347893, 3348168, 3353273, 3356...
我想首先對other_ids列中的陣列中的元素進行洗牌,然后創建一個新列,在其中我從不在列new_id的陣列中采樣一個 id 。
最后結果:other_ids target_id other_ids
target_id other_ids new_id
3733345 [3731634, 3729995, 3728014, 3708332, 3720... 3708332
3725312 [3711541, 3726052, 3733763, 900056057, 371... 900056057
3717114 [3701718, 3713481, 3715433, 3714825, 3731... 3250400
3408996 [3405896, 3250400, 3237054, 3242492, 3256... 3237054
3354970 [3354969, 3347893, 3348168, 3353273, 3356... 3353273
有什么建議?謝謝。
uj5u.com熱心網友回復:
你可以試試這個——
df = df.withColumn('new_id', F.element_at(
F.shuffle(
F.array_except(F.col('other_ids'), F.array(F.col('target_id')))
),
1
))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/450580.html
標籤:阿帕奇火花 pyspark apache-spark-sql
上一篇:無法訪問上傳到Databricks社區版Runtime9.1上的dbfs的檔案。嘗試了同樣無效的dbutils.fs.cp解決方法
