Apache spark 如何转置dataframe来计算值是否存在的标志?
我有一个这样的数据名声Apache spark 如何转置dataframe来计算值是否存在的标志?,apache-spark,dataframe,pyspark,apache-spark-sql,Apache Spark,Dataframe,Pyspark,Apache Spark Sql,我有一个这样的数据名声 Sno|UserID|TypeExp 1|JAS123|MOVIE 2|ASP123|GAMES 3|JAS123|CLOTHING 4|DPS123|MOVIE 5|DPS123|CLOTHING 6|ASP123|MEDICAL 7|JAS123|OTH 8|POQ133|MEDICAL ....... 10000|DPS123|OTH UserID是标识用户的列,TypeExp列定义了该月的支出类型,现在我有了5种可能的支出,即 TypeExpList=[电影、游
Sno|UserID|TypeExp
1|JAS123|MOVIE
2|ASP123|GAMES
3|JAS123|CLOTHING
4|DPS123|MOVIE
5|DPS123|CLOTHING
6|ASP123|MEDICAL
7|JAS123|OTH
8|POQ133|MEDICAL
.......
10000|DPS123|OTH
UserID是标识用户的列,TypeExp列定义了该月的支出类型,现在我有了5种可能的支出,即
TypeExpList=[电影、游戏、服装、医疗、其他]
现在我想把它转换成一个用户级的数据帧,其中有一个0或1二进制变量存储信息天气与否,用户“X”已经完成了上述类型的支出
例如,在上面的快照中,数据帧输出应该如下所示
User| TypeExpList #Type list is this array corresponding entry's [MOVIE,GAMES,CLOTHING,MEDICAL,OTH]
JAS123 |[1,0,1,0,1] #since user has done expenditure on Movie,CLOTHING,OTHER Category
ASP123 |[0,1,0,1,0] #since User expenditure on GAMES & MEDICAL
DPS123 |[1,0,1,0,1] #since user expenditure on MOVIE,CLOTHING & OTHER
POQ133 |[0,0,0,1,0] #since User Expenditure on MEDICAL only
解决方案在Scala中,但在PySpark中也应该类似,因为它使用DSL。Pivot可用于Spark 1.6+
val pivotDf = df.groupBy($"userid").pivot("typeexp").agg(count($"typeexp") )
pivotDf.show
+------+--------+-----+-------+-----+---+
|userid|CLOTHING|GAMES|MEDICAL|MOVIE|OTH|
+------+--------+-----+-------+-----+---+
|DPS123| 1| 0| 0| 1| 0|
|JAS123| 1| 0| 0| 1| 1|
|ASP123| 0| 1| 1| 0| 0|
|POQ133| 0| 0| 1| 0| 0|
+------+--------+-----+-------+-----+---+
pivotDf.selectExpr("userid", "array(movie, games, clothing, medical,oth) as TypExpList")
.show
+------+---------------+
|userid| TypExpList|
+------+---------------+
|DPS123|[1, 0, 1, 0, 0]|
|JAS123|[1, 0, 1, 0, 1]|
|ASP123|[0, 1, 0, 1, 0]|
|POQ133|[0, 0, 0, 1, 0]|
+------+---------------+
这是您的输入数据集
$ cat input.csv
Sno|UserID|TypeExp
1|JAS123|MOVIE
2|ASP123|GAMES
3|JAS123|CLOTHING
4|DPS123|MOVIE
5|DPS123|CLOTHING
6|ASP123|MEDICAL
7|JAS123|OTH
8|POQ133|MEDICAL
这样,您就可以在UserID
上的groupBy
上进行pivot
val bins = spark
.read
.option("sep", "|")
.option("header", true)
.csv("input.csv")
.groupBy("UserID")
.pivot("TypeExp")
.count
.na
.fill(0)
scala> bins.show
+------+--------+-----+-------+-----+---+
|UserID|CLOTHING|GAMES|MEDICAL|MOVIE|OTH|
+------+--------+-----+-------+-----+---+
|POQ133| 0| 0| 1| 0| 0|
|JAS123| 1| 0| 0| 1| 1|
|DPS123| 1| 0| 0| 1| 0|
|ASP123| 0| 1| 1| 0| 0|
+------+--------+-----+-------+-----+---+
你有你的0
s和1
s。最后一个技巧是在列上使用数组
,以构建适当的输出,其中位置表示支出
val solution = bins.select(
$"UserID" as "User",
array("MOVIE","GAMES","CLOTHING","MEDICAL","OTH") as "TypeExpList")
scala> solution.show
+------+---------------+
| User| TypeExpList|
+------+---------------+
|POQ133|[0, 0, 0, 1, 0]|
|JAS123|[1, 0, 1, 0, 1]|
|DPS123|[1, 0, 1, 0, 0]|
|ASP123|[0, 1, 0, 1, 0]|
+------+---------------+
假设一项支出可能发生零次、一次或多次,
count
超过支出可能会给出0
、1
或更高的数字
您可以使用UDF对值进行二值化,并确保只使用0
s和1
s
val binarizer = udf { count: Long => if (count > 0) 1 else 0 }
val binaryCols = bins
.columns
.filterNot(_ == "UserID")
.map(col)
.map(c => binarizer(c) as c.toString)
val selectCols = ($"UserID" as "User") +: binaryCols
val solution = bins
.select(selectCols: _*)
.select(
$"User",
array("MOVIE","GAMES","CLOTHING","MEDICAL","OTH") as "TypeExpList")
scala> solution.show
+------+---------------+
| User| TypeExpList|
+------+---------------+
|POQ133|[0, 0, 0, 1, 0]|
|JAS123|[1, 0, 1, 0, 1]|
|DPS123|[1, 0, 1, 0, 0]|
|ASP123|[0, 1, 0, 1, 0]|
+------+---------------+
交叉表
将完成大部分工作:
val table = df.stat.crosstab("UserID", "TypeExp")
+--------------+--------+-----+-------+-----+---+
|UserID_TypeExp|CLOTHING|GAMES|MEDICAL|MOVIE|OTH|
+--------------+--------+-----+-------+-----+---+
| ASP123| 0| 1| 1| 0| 0|
| DPS123| 1| 0| 0| 1| 0|
| JAS123| 1| 0| 0| 1| 1|
| POQ133| 0| 0| 1| 0| 0|
+--------------+--------+-----+-------+-----+---+
并且可以与强类型API很好地结合:
table.map(_.toSeq match {
case Seq(id: String, cnts @ _*) =>
(id, cnts.map(c => if(c != 0) 1 else 0))}).toDF("UserId", "TypeExp")
+------+---------------+
|UserId| TypeExp|
+------+---------------+
|ASP123|[0, 1, 1, 0, 0]|
|DPS123|[1, 0, 0, 1, 0]|
|JAS123|[1, 0, 0, 1, 1]|
|POQ133|[0, 0, 1, 0, 0]|
+------+---------------+