我有一个PySpark数据帧,由以下列组成:
id Age
1 30
2 25
3 21
我有以下年龄段:[20, 24, 27, 30]
。你知道吗
我的预期结果:
id Age age_bucket age_27_30 age_24_27 age_20_24
1 30 (27-30] 1 0 0
2 25 (24-27] 0 1 0
3 21 (20-24] 0 0 1
我的当前代码:
from pyspark.ml.feature import Bucketizer
bucketizer = Bucketizer(splits=[ 20,24,27,30 ],inputCol="Age", outputCol="age_bucket")
df1 = bucketizer.setHandleInvalid("keep").transform(df)
使用
OneHotEncoderEstimator()
如果您希望得到与您的问题完全相同的结果,
OneHotEstimatorEncoder
如果没有其他一些奇特的映射技巧,将无法工作。你知道吗我会在这里使用连接:
输出:
相关问题 更多 >
编程相关推荐