Pyspark - 如何从 DataFrame 列中获取随机值 [英] Pyspark - How to get random values from a DataFrame column
本文介绍了Pyspark - 如何从 DataFrame 列中获取随机值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 DataFrame 中有一个列,我需要在 Pyspark 中选择 3 个随机值.有人可以帮我吗?
I have one column in a DataFrame which I need to select 3 random values in Pyspark. Could anyone help-me, please?
+---+
| id|
+---+
|123|
|245|
| 12|
|234|
+---+
愿望:
从该列获得 3 个随机值的数组:
Array with 3 random values get from that column:
**output**: [123, 12, 234]
推荐答案
您可以先使用 rand()
函数进行随机排序:
You can order in random order using rand()
function first:
df.select('id').orderBy(rand()).limit(3).collect()
有关 rand()
函数的更多信息,请查看 pyspark.sql.functions.rand.
For more information on rand()
function, check out pyspark.sql.functions.rand.
这篇关于Pyspark - 如何从 DataFrame 列中获取随机值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文