我是PySpark的新手,我有下面的任务,我在那里挣扎。我试过很少接触他们,但他们都没有正常工作。数据如下:
id|numb_of_count|
1|3|
2|5|
3|6|
4|2|
5|0|
6|15|
7|8|
8|99|
我希望取得以下成果:
id|numb_of_count|banding|
1|3|3-5|
2|5|3-5|
3|6|6-10|
4|2|2|
5|0|0|
6|15|+11|
7|8|6-10|
8|99|+11|
由于我有一个很大的数据集,如何才能以最有效的方式实现这一点?
发布于 2018-07-09 15:20:23
在火星雨中,时/否则等于if/ are。如果df
是您的实际数据,那么:
new_df = df.withColumn('banding', when(col('numb_of_count') <3,col('numb_of_count')).when(col('numb_of_count') <=5 , '3-5').when(col('numb_of_count') <= 10, '6-10').otherwise('+11'))
df.withColumn
df.withColumn
以第一个参数作为新列的名称向框架中添加一个新列。更多信息这里
什么时候/什么时候
类似于if/else,更多信息这里
这是一个很好的回答,可以了解更多关于何时/其他方面的信息。
https://stackoverflow.com/questions/51247247
复制相似问题