python - PySpark sampleBy 使用多列

标签 python python-2.7 apache-spark pyspark

我想对 PySpark 上的数据框进行分层抽样。有一个 sampleBy(col, fractions, seed=None) 函数,但它似乎只使用一列作为层。有什么方法可以将多个列用作一个层吗?

最佳答案

基于答案here

将其转换为 python 后,我认为答案可能如下所示:

#create a dataframe to use
df = sc.parallelize([ (1,1234,282),(1,1396,179),(2,8620,178),(3,1620,191),(3,8820,828) ] ).toDF(["ID","X","Y"])

#we are going to use the first two columns as our key (strata)
#assign sampling percentages to each key # you could do something cooler here
fractions = df.rdd.map(lambda x: (x[0],x[1])).distinct().map(lambda x: (x,0.3)).collectAsMap()

#setup how we want to key the dataframe
kb = df.rdd.keyBy(lambda x: (x[0],x[1]))

#create a dataframe after sampling from our newly keyed rdd
#note, if the sample did not return any values you'll get a `ValueError: RDD is empty` error

sampleddf = kb.sampleByKey(False,fractions).map(lambda x: x[1]).toDF(df.columns)
sampleddf.show()
+---+----+---+
| ID|   X|  Y|
+---+----+---+
|  1|1234|282|
|  1|1396|179|
|  3|1620|191|
+---+----+---+
#other examples
kb.sampleByKey(False,fractions).map(lambda x: x[1]).toDF(df.columns).show()
+---+----+---+
| ID|   X|  Y|
+---+----+---+
|  2|8620|178|
+---+----+---+


kb.sampleByKey(False,fractions).map(lambda x: x[1]).toDF(df.columns).show()
+---+----+---+
| ID|   X|  Y|
+---+----+---+
|  1|1234|282|
|  1|1396|179|
+---+----+---+

这就是您要找的东西吗?

关于python - PySpark sampleBy 使用多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43878019/

相关文章:

apache-spark - 如何将连续 3 行视为一个数据集加载数据集?

Python 子进程给出语法错误但从 shell 运行

python - Django 1.7 的重构可调用

python - 如何模拟线程中的内置模块

python - 当指定面色时,边缘线在 mplot3d 冲浪中消失

python - 结合多个while语句python

scala - 为什么在使用模式查询时所有字段都为空?

python - 如何在 Python 中获取 UTC 日期字符串?

python - 我的 python 循环数据帧随着时间的推移而减慢

java - 简单的节点发现方法