Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dataframe pyspark:计算连续1/0的数量,如果streak为短/长,则对其进行更改_Dataframe_Search_Replace_Pyspark - Fatal编程技术网

Dataframe pyspark:计算连续1/0的数量,如果streak为短/长,则对其进行更改

Dataframe pyspark:计算连续1/0的数量,如果streak为短/长,则对其进行更改,dataframe,search,replace,pyspark,Dataframe,Search,Replace,Pyspark,我在集群上使用大型pyspark数据帧,需要编写一个函数: 查找特定列中连续零的行,如果该条纹短于300行,则将它们全部更改为1和 然后在该列中查找连续的周期,如果连续的周期短于1800行,则将其全部设置为0 每一行都有一个唯一的时间戳,我可以根据它进行排序 有什么方法可以做到这一点吗?是的,你可以按照这个例子来做,我搜索了少于3个零的打击,并将它们转换为1: column = 'data' date_column = 'timestamp' min_consecutive_rows = 3

我在集群上使用大型pyspark数据帧,需要编写一个函数:

  • 查找特定列中连续零的行,如果该条纹短于300行,则将它们全部更改为1和

  • 然后在该列中查找连续的周期,如果连续的周期短于1800行,则将其全部设置为0

  • 每一行都有一个唯一的时间戳,我可以根据它进行排序


    有什么方法可以做到这一点吗?

    是的,你可以按照这个例子来做,我搜索了少于3个零的打击,并将它们转换为1:

    column = 'data'
    date_column = 'timestamp'
    min_consecutive_rows = 3
    search_num = 0
    set_to = 1
    
    df = df.withColumn('binary', F.when(col(column)==search_num, 1).otherwise(0))\
    .withColumn('start_streak', F.when(col('binary') != F.lead('binary', -1).over(w), 1).otherwise(0))\
    .withColumn('streak_id', F.sum('start_streak').over(Window.orderBy(date_column)))\
    .withColumn("streak_counter", F.row_number().over(Window.partitionBy("streak_id").orderBy(date_column)))\
    .withColumn('max_streak_counter', F.max('streak_counter').over(Window.partitionBy("streak_id")))\
    .withColumn(column, F.when((col('binary')==1) & (col('max_streak_counter') < min_consecutive_rows), set_to).otherwise(col(column)))
    
    对于第二个要点只需更改:列为“数据输出”,最小连续行为1800,搜索数为1,将参数设置为0,然后重复上述代码


    有关条纹计算的更多详细信息,请访问pandas中具有类似逻辑的功能。

    为了让人们快速理解,最好共享输入和输出数据。查看窗口功能以实现此功能。这里有一个例子。
    |           timestamp|data|binary|start_streak|streak_id|streak_counter|max_streak_counter|data_output|
    +--------------------+----+------+------------+---------+--------------+------------------+-----------+
    |2020-11-11 15:52:...|   1|     0|           0|        0|             1|                 5|          1|
    |2020-11-12 15:52:...|   2|     0|           0|        0|             2|                 5|          2|
    |2020-11-13 15:52:...|   3|     0|           0|        0|             3|                 5|          3|
    |2020-11-14 15:52:...|   4|     0|           0|        0|             4|                 5|          4|
    |2020-11-15 15:52:...|   1|     0|           0|        0|             5|                 5|          1|
    |2020-11-16 15:52:...|   0|     1|           1|        1|             1|                 2|          1|
    |2020-11-17 15:52:...|   0|     1|           0|        1|             2|                 2|          1|
    |2020-11-18 15:52:...|   1|     0|           1|        2|             1|                 1|          1|
    |2020-11-19 15:52:...|   0|     1|           1|        3|             1|                 4|          0|
    |2020-11-20 15:52:...|   0|     1|           0|        3|             2|                 4|          0|
    |2020-11-21 15:52:...|   0|     1|           0|        3|             3|                 4|          0|
    |2020-11-22 15:52:...|   0|     1|           0|        3|             4|                 4|          0|
    +--------------------+----+------+------------+---------+--------------+------------------+-----------+