Bha*_*th 4 scala dataframe apache-spark apache-spark-sql
我们可以使用 Spark-SQL 或 scala 检查 Spark 数据帧中的每一列是否都包含某个字符串(例如“Y”)吗?
我已经尝试过以下方法,但认为它不能正常工作。
df.select(df.col("*")).filter("'*' =='Y'")
Run Code Online (Sandbox Code Playgroud)
谢谢,赛
您可以执行以下操作来保留所有列都包含“Y”的行:
//Get all columns
val columns: Array[String] = df.columns
//For each column, keep the rows with 'Y'
val seqDfs: Seq[DataFrame] = columns.map(name => df.filter(s"$name == 'Y'"))
//Union all the dataframes together into one final dataframe
val output: DataFrame = seqDfs.reduceRight(_ union _)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14384 次 |
| 最近记录: |