代码之家  ›  专栏  ›  技术社区  ›  default_settings

如何基于值pyspark创建带区列

  •  0
  • default_settings  · 技术社区  · 6 年前

    我是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|
    

    由于我有一个大的数据集,如何以最有效的方式实现这一点?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Rahul Chawla cbiqih    6 年前

    在pyspark中,当/否则等同于if/else。如果 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.带柱

    df.withColumn 将新列添加到框架中,第一个参数作为新列的名称。更多信息 here

    何时/以其他方式

    类似于if/else,更多信息 here

    这是一个很好的 answer 了解更多时间/其他方面的信息。