dataframe - Pyspark:如何为另一列中具有相同值的所有行设置相同的id?

标签 dataframe apache-spark pyspark

我有一个像这样的数据集:

  +----------+------------+
  |id        |event       |
  +----------+------------+
  | 1        |A           |
  | 2        |B           |
  | 3        |C           |
  | 4        |C           |
  | 5        |A           |
  | 6        |D           |
  | 7        |B           |
  +----------+------------+

我想修改 id 或添加另一列,其中“事件”列中的所有相等值都具有相同的 id。我希望这些行保持与现在相同的顺序。

这就是我希望数据最终呈现的方式(“id”的值并不重要,只要它对于每个事件都是唯一的)

  +----------+------------+
  |id        |event       |
  +----------+------------+
  | 1        |A           |
  | 2        |B           |
  | 3        |C           |
  | 3        |C           |
  | 1        |A           |
  | 4        |D           |
  | 2        |B           |
  +----------+------------+

最佳答案

更新

添加monotonically_increasing_id()设置 id 后查看原始输入中的数据:

The generated ID is guaranteed to be monotonically increasing and unique, but not consecutive. The current implementation puts the partition ID in the upper 31 bits, and the record number within each partition in the lower 33 bits. The assumption is that the data frame has less than 1 billion partitions, and each partition has less than 8 billion records.

output_df = (input_df
             .withColumn('order', f.monotonically_increasing_id())
             .withColumn('id', f.first('id').over(Window.partitionBy('event'))))
output_df.sort('order').show()

+---+-----+-----------+
| id|event|      order|
+---+-----+-----------+
|  1|    A| 8589934592|
|  2|    B|17179869184|
|  3|    C|25769803776|
|  3|    C|34359738368|
|  1|    A|42949672960|
|  6|    D|51539607552|
|  2|    B|60129542144|
+---+-----+-----------+

要“保留”数据帧顺序,请创建另一列并保持 id 完整,以便在需要时进行排序:

from pyspark.sql import Window
import pyspark.sql.functions as f

input_df = spark.createDataFrame([
  [1, 'A'],
  [2, 'B'],
  [3, 'C'],
  [4, 'C'],
  [5, 'A'],
  [6, 'D'],
  [7, 'B']
], ['id', 'event'])

output_df = input_df.withColumn('group_id', f.first('id').over(Window.partitionBy('event')))
output_df.sort('id').show()

+---+-----+--------+
| id|event|group_id|
+---+-----+--------+
|  1|    A|       1|
|  2|    B|       2|
|  3|    C|       3|
|  4|    C|       3|
|  5|    A|       1|
|  6|    D|       6|
|  7|    B|       2|
+---+-----+--------+

关于dataframe - Pyspark:如何为另一列中具有相同值的所有行设置相同的id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69028786/

相关文章:

python - 如何使用groupby获取与列的最大值对应的所有行

r - 如何在R中从具有多个条件的一个数据帧创建多个数据帧

apache-spark - 如何使用 Terraform 部署 EMR Terraform,一个开箱即用的简单工作示例

apache-spark - 列变换后的 Pyspark 随机森林特征重要性映射

python - 了解 Spark 中的 treeReduce()

azure - 如何将spark-csv包添加到Azure上的jupyter服务器以与iPython一起使用

python - 如何检查字符串中是否包含某个单词?

python - Pandas groupby 根据 groupby 中值的数量进行条件过滤

hadoop - YARN可以抢占Spark驱动程序吗?

python - 将每个元素视为元组时,在 PySpark 中加入 2 个 RDD