简体   繁体   English

我们如何使用 Scala 在 spark 中添加列值?

[英]How do we add column value in spark using Scala?

I have data like this我有这样的数据

+-------------+--------+--------+
|         COl1|    Col2|    COL3|
+-------------+--------+--------+
|A .......... |   56102|   45991|
|B........... |   25336|   23099|
+-------------+--------+--------+

it should be like this应该是这样的

+-------------+--------+--------+
|         COl1|    Col2|    COL3|
+-------------+--------+--------+
|A .......... |   56102|   45991|
|B........... |   25336|   23099|
|Total....... |   58368|   69090|
+-------------+--------+--------+

need a row with Total and the value should be the sum of reaming row in the dataframe.需要一行总和,该值应该是数据框中铰孔行的总和。

You can use aggregation functions to compute the sums, and a union to append them at the end of the original df.您可以使用聚合函数来计算总和,并使用联合将它们附加到原始 df 的末尾。 For it to work, you just need to make sure that the names of the columns coincide.要使其工作,您只需要确保列的名称一致。

It would go like this:它会是这样的:

val df = Seq(("A", 56102, 45991), ("B",  25336, 23099))
    .toDF("COL1", "COL2", "COL3")

val sums = df.select(lit("Total") as "COL1", sum('COL2) as "COL2", sum('COL3) as "COL3")
df.union(sums).show()
+-----+-----+-----+
| COL1| COL2| COL3|
+-----+-----+-----+
|    A|56102|45991|
|    B|25336|23099|
|Total|81438|69090|
+-----+-----+-----+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scala Spark,如何为列添加值 - Scala Spark, how to add value to the column 如何进行 groupby 排名并将其作为列添加到 spark scala 中的现有 dataframe? - How to do a groupby rank and add it as a column to existing dataframe in spark scala? 如何使用Spark在Scala中返回多列键和值对 - How do I return Multi-Column Key and Value pairs in Scala using Spark Spark,在Scala中添加具有相同值的新列 - Spark, add new Column with the same value in Scala 在spark scala 中将行合并为单个struct 列存在效率问题,我们如何做得更好? - Merging rows into a single struct column in spark scala has efficiency problems, how do we do it better? 如何使用databrick将属性和值添加到scala(spark)中的xml标签 - How to add attribute and value to xml tag in scala(spark) by using databrick 使用scala查找spark中列值的差异 - Find difference of column value in spark using scala 我们如何比较 spark scala 中的两个数据框以找出这两个文件之间的差异,哪一列? 和价值? - How can we compare two dataframes in spark scala to find difference between these 2 files, which column ?? and value? 在 Scala Spark 中,如何拆分一列,使前半部分成为列名,第二部分成为列值? - How do you split a column such that first half becomes the column name and the second the column value in Scala Spark? 将具有文字值的新列添加到 Spark Scala 中 Dataframe 中的结构列 - Add new column with literal value to a struct column in Dataframe in Spark Scala
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM