使用pyspark从1开始按行号增加列

时间:2019-05-21 11:35:24

标签: pyspark

我想创建一个名为“ id”的列,该列将具有用于最终配对生成的行号。 “ id”列

我使用以下方式在python中完成了此操作。谁能建议如何在pyspark中做到这一点。

con_2['id'] = range(1, 1+len(con_2))
len(con_2.customer_play_id.unique()) 

我的Pyspark代码在下面,但不起作用

from pyspark.sql.types import IntegerType
slen = udf(lambda s: len(s), IntegerType())
con_2 = con_2.withColumn('id', F.length(con_2.customer_play_id))

预期输出应为(Id是我要添加的列) df

id  col1 col2
1   X      Y
2   y1     y4
3   y2     y7
4   y3     y8

1 个答案:

答案 0 :(得分:0)

db.collection.aggregate([
    {
        $project: {
            properties: {
                $reduce: {
                    input: "$config",
                    initialValue: [],
                    in: {
                        $concatArrays: [
                            "$$value",
                            {
                                $map: {
                                    input: {
                                        $objectToArray: "$$this"
                                    },
                                    in: "$$this.k"
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
])
相关问题