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

非周期性数据值移位处理问题:pandas.Series.shift()无法满足需求

Solution for Filling prev_value Based on Consecutive Groups of new_value

Looking at your data and desired output, what you need is to fill prev_value with the value of the previous consecutive group from new_value—with the first group using its own value as the prev_value. Here's a straightforward way to do this with pandas, even without periodic data:

Step-by-Step Implementation

  • Convert your data to a pandas DataFrame
    First, let's get your raw data into a DataFrame for easier manipulation:

    import pandas as pd
    
    data = {
        'new_value': ['100', '100', '250', '250', '250', '50', '90', '90', '90', '90'],
        'prev_value': ['None'] * 10
    }
    df = pd.DataFrame(data)
    
  • Identify consecutive groups in new_value
    We can create a group identifier by checking where the value of new_value changes from the previous row. Using a shift comparison will help us spot these changes, then we can cumulative sum to assign unique group IDs:

    # Create a boolean mask where new_value changes from the prior row
    group_mask = df['new_value'] != df['new_value'].shift(1)
    # Assign unique group IDs starting from 0
    df['group_id'] = group_mask.cumsum() - 1
    
  • Map each group to its corresponding prev_value
    Now, we'll create a lookup dictionary that maps each group ID to its appropriate prev_value:

    • For the first group (ID 0), prev_value is the group's own new_value
    • For subsequent groups, prev_value is the new_value of the previous group
    # Get the unique value for each group
    group_values = df.groupby('group_id')['new_value'].first().reset_index()
    # Shift group values to get the previous group's value
    group_values['prev_group_value'] = group_values['new_value'].shift(1)
    # Fill the first group's prev with its own value
    group_values.loc[0, 'prev_group_value'] = group_values.loc[0, 'new_value']
    # Convert to a dictionary for quick lookup
    prev_lookup = group_values.set_index('group_id')['prev_group_value'].to_dict()
    
  • Fill the prev_value column using the lookup
    Finally, we map the group_id to the lookup dictionary to populate prev_value:

    df['prev_value'] = df['group_id'].map(prev_lookup)
    # Optional: drop the group_id column if you don't need it
    df = df.drop('group_id', axis=1)
    

Verify the Result

If you print out df, you'll get exactly your desired output:

new_value prev_value
0       100        100
1       100        100
2       250        100
3       250        100
4       250        100
5        50        250
6        90         50
7        90         50
8        90         50
9        90         50

To convert it back to the dictionary format you want:

exp_result = df.to_dict('list')

This approach works regardless of how many consecutive values are in each group, and doesn't rely on periodicity—perfect for your use case!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 13:27:49