scala - Spark数据帧:How to add a index Column : Aka Distributed Data Index

标签 scala apache-spark dataframe apache-spark-sql

我从 csv 文件读取数据,但没有索引。

我想添加从 1 到行号的列。

我该怎么办,谢谢(scala)

最佳答案

使用 Scala,您可以使用:

import org.apache.spark.sql.functions._ 

df.withColumn("id",monotonicallyIncreasingId)

可以引用这个exemple和 scala docs .

通过 Pyspark,您可以使用:

from pyspark.sql.functions import monotonically_increasing_id 

df_index = df.select("*").withColumn("id", monotonically_increasing_id())

关于scala - Spark数据帧:How to add a index Column : Aka Distributed Data Index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43406887/

相关文章:

java - 无法使用 scala 构建 gRPC ManagedChannel

scala - 通过python脚本在命令行中执行sbt并输出到文件

apache-spark - Spark中未捕获的异常处理

python - Pandas:将自定义函数应用于组并将结果存储在每个组的新列中

python - Pandas - 如何对多个变量进行分组和拆分?

scala - 类的实例与其伴生对象之间的关系

scala - specs2 After 方法在示例之前运行

hadoop - 如何设置newAPIHadoopFile的分区数?

scala - reduceByKey 不是成员(member)

r - 在 R 中索引客户交易