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

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

问题描述

如何在 Scala 中创建一个具有 100 行和 3 列且随机整数值在 (1, 100) 范围内的 Spark DataFrame?

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天全站免登陆