如何生成具有随机内容和N行的DataFrame? [英] How to generate a DataFrame with random content and N rows?

查看:473
本文介绍了如何生成具有随机内容和N行的DataFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Scala中创建100行3列的Spark DataFrame,它们的随机整数值在(1,100)范围内?

How can I create a Spark DataFrame in Scala with 100 rows and 3 columns that have random integer values in range (1, 100)?

我知道如何手动创建DataFrame,但是我无法将其自动化:

I know how to create a DataFrame manually, but I cannot automate it:

val df = sc.parallelize(Seq((1,20, 40), (60, 10, 80), (30, 15, 30))).toDF("col1", "col2", "col3") 

推荐答案

在这里,Seq.fill是您的朋友:

def randomInt1to100 = scala.util.Random.nextInt(100)+1

val df = sc.parallelize(
  Seq.fill(100){(randomInt1to100,randomInt1to100,randomInt1to100)}
).toDF("col1", "col2", "col3")

这篇关于如何生成具有随机内容和N行的DataFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆