非周期性数据值移位处理问题:pandas.Series.shift()无法满足需求
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 ofnew_valuechanges 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() - 1Map each group to its corresponding
prev_value
Now, we'll create a lookup dictionary that maps each group ID to its appropriateprev_value:- For the first group (ID 0),
prev_valueis the group's ownnew_value - For subsequent groups,
prev_valueis thenew_valueof 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()- For the first group (ID 0),
Fill the
prev_valuecolumn using the lookup
Finally, we map thegroup_idto the lookup dictionary to populateprev_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

