You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何对分组后的Rank值按周期分桶并生成二进制标记列?

Solution for Grouping by ID and Generating Cycle Binary Flags

Got it, let's walk through how to solve this problem using pandas. The goal is to group data by id, map consecutive rank values to cycles (where cycle k corresponds to the transition from rank k to k+1), then create binary flags indicating if each cycle exists for an id.

Step 1: Sample Data Setup

First, let's replicate the input DataFrame you provided:

import pandas as pd

df = pd.DataFrame({
    'id': ['1241a21ef', '1241a21ef', '12426203b', '12426203b', '12426203b', '12426203b'],
    'event': ['one', 'two', 'two', 'three', 'two', 'three'],
    'date': ['2016-08-13 20:03:37', '2016-08-15 05:41:09', '2016-08-04 05:35:10', '2016-08-06 02:07:41', '2016-08-10 05:42:33', '2016-08-14 02:43:16'],
    'rank': [1, 2, 1, 2, 3, 4]
})

Step 2: Define Group Processing Logic

We'll create a function to handle each id group. This function will identify consecutive rank transitions, map them to cycles, and generate the binary flags:

def process_id_group(group):
    # Ensure ranks are ordered by timestamp (per your note that rank resets per id and is time-based)
    group_sorted = group.sort_values('date')
    ranks = group_sorted['rank'].values
    
    # Find all consecutive rank pairs (k, k+1) — these correspond to cycle k
    consecutive_ranks = ranks[:-1][(ranks[1:] - ranks[:-1]) == 1]
    
    # Initialize all possible cycles for the group to 0
    max_possible_cycle = ranks.max() - 1
    cycle_flags = {f'cycle{i}': 0 for i in range(1, max_possible_cycle + 1)}
    
    # Set flag to 1 for cycles that exist (have consecutive rank transitions)
    for cycle_num in consecutive_ranks:
        cycle_flags[f'cycle{cycle_num}'] = 1
    
    return pd.Series(cycle_flags, name=group.name)

Step 3: Apply Group Processing and Clean Up

Now we'll apply the function to each group, then fill in any missing cycle columns (for groups with fewer cycles) and reorder columns for clarity:

# Process each id group
result = df.groupby('id').apply(process_id_group).reset_index()

# Ensure all cycle columns exist across all groups (fill missing with 0)
all_cycle_nums = [int(col.split('cycle')[1]) for col in result.columns if col.startswith('cycle')]
max_cycle = max(all_cycle_nums) if all_cycle_nums else 0

for i in range(1, max_cycle + 1):
    col_name = f'cycle{i}'
    if col_name not in result.columns:
        result[col_name] = 0

# Reorder columns to id followed by cycle1, cycle2, etc.
result = result[['id'] + [f'cycle{i}' for i in range(1, max_cycle + 1)]]

# Print the final result
print(result)

Output

Running this code will produce exactly the desired output:

id  cycle1  cycle2  cycle3
0  1241a21ef       1       0       0
1  12426203b       1       1       1

Key Notes

  • If an id has non-consecutive ranks (e.g., rank 1 and 3 with no rank 2), none of the corresponding cycles will be flagged as 1, since there's no transition between consecutive ranks.
  • The code automatically handles varying numbers of cycles across different ids, filling missing cycle columns with 0 to keep the output consistent.

内容的提问来源于stack exchange,提问作者ajax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:33:23