我有一个 Spark 数据框与列(年龄)。我需要编写一个pyspark脚本以将数据帧存储在10岁以下的年龄段(例如11-20岁,21-30岁...等)中,并找到每个年龄段条目的数量。通过这个
对于前:
我有以下数据框
+-----+
|age |
+-----+
| 21|
| 23|
| 35|
| 39|
+-----+
装桶后(预期)
+-----+------+
|age | count|
+-----+------+
|21-30| 2 |
|31-40| 2 |
+-----+------+
最佳答案
一种简单的计算方法是在基础RDD上计算直方图。
给定已知的年龄范围(幸运的是,这很容易组合在一起-在这里使用1、11、21等),生成直方图相当容易:
hist = df.rdd\
.map(lambda l: l['age'])\
.histogram([1, 11, 21,31,41,51,61,71,81,91])
这将返回一个带有“年龄范围”及其相应观察计数的元组,如下所示:
([1, 11, 21, 31, 41, 51, 61, 71, 81, 91],
[10, 10, 10, 10, 10, 10, 10, 10, 11])
然后,您可以使用以下方法将其转换回数据框:
#Use zip to link age_ranges to their counts
countTuples = zip(hist[0], hist[1])
#make a list from that
ageList = list(map(lambda l: Row(age_range=l[0], count=l[1]), countTuples))
sc.parallelize(ageList).toDF()
有关更多信息,请查看the RDD API中的
histogram
函数的文档。
关于python-3.x - 装满 Spark 数据帧-pyspark,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49516581/