[英]How to drop specific column and then select all columns from spark dataframe
[英]Select Specific Columns from Spark DataFrame
我已将 CSV 数据加载到 Spark DataFrame 中。
我需要将此数据帧切成两个不同的数据帧,其中每个数据帧都包含来自原始数据帧的一组列。
如何根据列选择一个子集到 Spark 数据帧中?
如果要将数据框拆分为两个不同的数据框,请使用所需的不同列对其进行两次选择。
val sourceDf = spark.read.csv(...)
val df1 = sourceDF.select("first column", "second column", "third column")
val df2 = sourceDF.select("first column", "second column", "third column")
请注意,这当然意味着 sourceDf 将被评估两次,因此如果它可以适合分布式内存并且您在两个数据帧中使用大部分列,那么缓存它可能是一个好主意。 它有许多您不需要的额外列,然后您可以先对其进行选择以选择您需要的列,以便将所有额外数据存储在内存中。
有多种选项(尤其是在 Scala 中)可以选择该 Dataframe 的列的子集。 以下几行显示了选项,其中大部分都记录在Column的 ScalaDocs 中:
import spark.implicits._
import org.apache.spark.sql.functions.{col, column, expr}
inputDf.select(col("colA"), col("colB"))
inputDf.select(inputDf.col("colA"), inputDf.col("colB"))
inputDf.select(column("colA"), column("colB"))
inputDf.select(expr("colA"), expr("colB"))
// only available in Scala
inputDf.select($"colA", $"colB")
inputDf.select('colA, 'colB) // makes use of Scala's Symbol
// selecting columns based on a given iterable of Strings
val selectedColumns: Seq[Column] = Seq("colA", "colB").map(c => col(c))
inputDf.select(selectedColumns: _*)
// Special cases
col("columnName.field") // Extracting a struct field
col("`a.column.with.dots`") // Escape `.` in column names.
// select the first or last 2 columns
inputDf.selectExpr(inputDf.columns.take(2): _*)
inputDf.selectExpr(inputDf.columns.takeRight(2): _*)
$
的使用是可能的,因为 Scala 提供了一个隐式类,该类使用$
方法将字符串转换为列:
implicit class StringToColumn(val sc : scala.StringContext) extends scala.AnyRef {
def $(args : scala.Any*) : org.apache.spark.sql.ColumnName = { /* compiled code */ }
}
通常,当您想将一个 DataFrame 派生为多个 DataFrame 时,如果您在创建其他 DataFrame 之前persist
原始 DataFrame,则可能会提高您的性能。 最后,您可以unpersist
原始 DataFrame。
请记住,列不会在编译时解析,而只会在与在查询执行的分析器阶段发生的目录的列名进行比较时进行解析。 如果您需要更强的类型安全性,您可以创建一个Dataset
。
为了完整起见,这里是用于尝试上述代码的 csv:
// csv file:
// colA,colB,colC
// 1,"foo","bar"
val inputDf = spark.read.format("csv").option("header", "true").load(csvFilePath)
// resulting DataFrame schema
root
|-- colA: string (nullable = true)
|-- colB: string (nullable = true)
|-- colC: string (nullable = true)
假设我们的父 Dataframe 有“n”列
我们可以创建“x”个子数据帧(在我们的例子中让我们考虑 2)。
可以根据需要从任何父 Dataframe 列中选择子 Dataframe 的列。
考虑源有10 列,我们希望拆分为2 个数据帧,其中包含从父数据帧引用的列。
可以使用select Dataframe API 决定子 Dataframe 的列
val parentDF = spark.read.format("csv").load("/path of the CSV file")
val Child1_DF = parentDF.select("col1","col2","col3","col9","col10").show()
val child2_DF = parentDF.select("col5", "col6","col7","col8","col1","col2").show()
请注意,子数据帧中的列数的长度可能不同,并且会小于父数据帧的列数。
我们还可以使用父数据帧中所需列的位置索引来引用列名,而无需提及真实名称
导入 spark 隐式首先充当帮助类,用于使用 $-notation 来访问使用位置索引的列
import spark.implicits._
import org.apache.spark.sql.functions._
val child3_DF = parentDF.select("_c0","_c1","_c2","_c8","_c9").show()
我们也可以根据特定条件选择列。 假设我们只想在子数据框中选择偶数列。 通过 even 我们指的是偶数索引列和索引从“0”开始
val parentColumns = parentDF.columns.toList
res0: List[String] = List(_c0, _c1, _c2, _c3, _c4, _c5, _c6, _c7,_c8,_c9)
val evenParentColumns = res0.zipWithIndex.filter(_._2 % 2 == 0).map( _._1).toSeq
res1: scala.collection.immutable.Seq[String] = List(_c0, _c2, _c4, _c6,_c8)
现在提供这些要从 parentDF 中选择的列。注意选择 API 需要 seq 类型参数。所以我们将“evenParentColumns”转换为 Seq 集合
val child4_DF = parentDF.select(res1.head, res1.tail:_*).show()
这将显示来自父 Dataframe 的偶数索引列。
| _c0 | _c2 | _c4 |_c6 |_c8 |
|ITE00100554|TMAX|空| E| 1 |
|TE00100554 |TMIN|空| E| 4 |
|GM000010962|PRCP|空| E| 7 |
所以现在我们在数据框中留下偶数列
同样,我们也可以对 Dataframe 列应用其他操作,如下所示
val child5_DF = parentDF.select($"_c0", $"_c8" + 1).show()
因此,通过前面提到的多种方式,我们可以选择 Dataframe 中的列。
解决了,只需使用数据框的select方法来选择列:
val df=spark.read.csv("C:\\Users\\Ahmed\\Desktop\\cabs_trajectories\\cabs_trajectories\\green\\2014\\green_tripdata_2014-09.csv")
val df1=df.select("_c0")
这将对数据框的第一列进行子集化
我喜欢 dehasis 方法,因为它允许我一步选择、重命名和转换列。 但是我不得不调整它以使其在 PySpark 中对我有用:
from pyspark.sql.functions import col
spark.read.csv(path).select(
col('_c0').alias("stn").cast('String'),
col('_c1').alias("wban").cast('String'),
col('_c2').alias("lat").cast('Double'),
col('_c3').alias("lon").cast('Double')
)
.where('_c2.isNotNull && '_c3.isNotNull && '_c2 =!= 0.0 && '_c3 =!= 0.0)
只需使用 select select
您就可以选择特定的列,为它们提供可读的名称并进行转换。 例如像这样:
spark.read.csv(path).select(
'_c0.alias("stn").cast(StringType),
'_c1.alias("wban").cast(StringType),
'_c2.alias("lat").cast(DoubleType),
'_c3.alias("lon").cast(DoubleType)
)
.where('_c2.isNotNull && '_c3.isNotNull && '_c2 =!= 0.0 && '_c3 =!= 0.0)
您可以使用以下代码根据索引(位置)选择列。 您可以更改变量 colNos 的数字以仅选择那些列
import org.apache.spark.sql.functions.col
val colNos = Seq(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35)
val Df_01 = Df.select(colNos_01 map Df.columns map col: _*)
Df_01.show(20, false)
问题是在加入其他数据帧后选择数据帧上的列。
我在下面尝试并从连接的数据框中选择salaryDf 的列。
希望这会有所帮助
val empDf=spark.read.option("header","true").csv("/data/tech.txt")
val salaryDf=spark.read.option("header","true").csv("/data/salary.txt")
val joinData= empDf.join(salaryDf,empDf.col("first") === salaryDf.col("first") and empDf.col("last") === salaryDf.col("last"))
//**below will select the colums of salaryDf only**
val finalDF=joinData.select(salaryDf.columns map salaryDf.col:_*)
//same way we can select the columns of empDf
joinData.select(empDf.columns map empDf.col:_*)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.