【问题标题】:Use of underscore and map fuction in dataframes/scala [duplicate]在数据框/scala中使用下划线和映射函数[重复]
【发布时间】:2018-11-21 08:56:44
【问题描述】:

我正在尝试了解map 函数的使用以及下面代码中的下划线_keysList[String]dfDateFrame。我运行了一个示例,发现 listOfVal 是一个列类型列表,但是有人可以帮助解释它是如何工作的吗?在这种情况下_ 是什么意思,map 函数应用了什么?非常感谢

val listOfVal = keys.map(df(_))

ps:我已经阅读了建议的两个问题,但我认为它们是不同的用例

【问题讨论】:

标签: scala function apache-spark dataframe


【解决方案1】:

在 Scala 中,_ 可以充当匿名函数的占位符。例如:

List("A", "B", "C").map(_.toLowerCase)
// `_.toLowerCase` represents anonymous function `x => x.toLowerCase`
// res1: List[String] = List(a, b, c)

List(1, 2, 3, 4, 5).foreach(print(_))
// `print(_)` represents anonymous function `x => print(x)`
// res2: 12345

在您的示例代码中,keys.map(df(_)) 相当于:

keys.map(c => df(c))

假设您的 keys 是一个列名列表:

List[String]("col1", "col2", "col3")

然后它只是被映射到:

List[Column](df("col1"), df("col2"), df("col3"))

【讨论】:

  • 谢谢@Leo C 我认为它完美地解释了我的问题。接受答案并投票。
猜你喜欢
  • 2021-07-04
  • 2020-01-05
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
相关资源
最近更新 更多