gpt4 book ai didi

Scala - 使用 "endsWith"过滤数据帧

转载 作者:行者123 更新时间:2023-12-01 19:35:36 32 4
gpt4 key购买 nike

给定一个 DataFrame :

 val df = sc.parallelize(List(("Mike","1986","1976"), ("Andre","1980","1966"), ("Pedro","1989","2000")))
.toDF("info", "year1", "year2")
df.show

+-----+-----+-----+
| info|year1|year2|
+-----+-----+-----+
| Mike| 1986| 1976|
|Andre| 1980| 1966|
|Pedro| 1989| 2000|
+-----+-----+-----+

我尝试过滤所有以 df 结尾的 6 值,但得到异常。
我试过了 :
  val filtered = df.filter(df.col("*").endsWith("6"))
org.apache.spark.sql.catalyst.analysis.UnresolvedException: Invalid call to dataType on unresolved object, tree: ResolvedStar(info#20, year1#21, year2#22)

我也试过这个:
val filtered = df.select(df.col("*")).filter(_ endsWith("6"))
error: missing parameter type for expanded function ((x$1) => x$1.endsWith("6"))

如何解决?谢谢

最佳答案

我不太确定您要做什么,但据我了解:

val df = sc.parallelize(List(("Mike","1986","1976"), ("Andre","1980","1966"), ("Pedro","1989","2000"))).toDF("info", "year1", "year2")
df.show
# +-----+-----+-----+
# | info|year1|year2|
# +-----+-----+-----+
# | Mike| 1986| 1976|
# |Andre| 1980| 1966|
# |Pedro| 1989| 2000|
# +-----+-----+-----+

val conditions = df.columns.map(df(_).endsWith("6")).reduce(_ or _)
df.withColumn("condition", conditions).filter($"condition" === true).drop("condition").show
# +-----+-----+-----+
# | info|year1|year2|
# +-----+-----+-----+
# |Andre| 1980| 1966|
# | Mike| 1986| 1976|
# +-----+-----+-----+

关于Scala - 使用 "endsWith"过滤数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41803989/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com