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

如何高效实现Pandas DataFrame按索引对应值逐行移位?

Efficient Row-wise Shifting for Large DataFrames

Hey there! When dealing with massive datasets, looping through each row with iloc[i].shift(m[i]) is definitely going to be a bottleneck—vectorized operations are the way to go here. Let's dive into a much faster approach using NumPy's array manipulation, which plays nicely with pandas under the hood.

The Core Idea

Instead of processing rows one by one, we can leverage broadcasting and index masking to shift all rows in a single vectorized operation. Here's the breakdown:

  1. Convert the DataFrame to a NumPy array for low-level, optimized manipulation.
  2. Create a grid of column indices paired with the shift values from m to identify which positions should be filled with NaN.
  3. Shift the data into the correct positions in bulk and fill the masked areas with NaN.
  4. Convert the resulting array back to a DataFrame with your original column names.

The Efficient Solution Code

import pandas as pd
import numpy as np

# Your sample data setup
sample_col = ["col1","col2","col3","col4", "col5"]
sample_data = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]]
sample = pd.DataFrame(data = sample_data, columns = sample_col)
m = [0,1,2,1]

# Convert DataFrame to a numpy array for fast operations
arr = sample.to_numpy()
n_rows, n_cols = arr.shape

# Create a 2D grid of column indices (repeated for each row)
col_indices = np.tile(np.arange(n_cols), (n_rows, 1))
# Reshape shift values to align with the column grid
shift_vals = np.array(m)[:, np.newaxis]

# Mask positions where column index is less than the row's shift value (these become NaN)
mask = col_indices < shift_vals
# Initialize shifted array with NaNs, then fill valid positions
shifted_arr = np.full_like(arr, np.nan)
shifted_arr[~mask] = arr[col_indices >= shift_vals]

# Convert back to a pandas DataFrame
result = pd.DataFrame(shifted_arr, columns=sample_col)
print(result)

Expected Output

col1  col2  col3  col4  col5
0   1.0   2.0   3.0   4.0   5.0
1   NaN   6.0   7.0   8.0   9.0
2   NaN   NaN  11.0  12.0  13.0
3   NaN  16.0  17.0  18.0  19.0

Why This Is Way Faster

  • Vectorized Operations: All logic runs in bulk via NumPy, which uses optimized C code under the hood—no slow Python-level loops dragging things down.
  • Minimal Overhead: We work directly with the underlying array instead of relying on pandas' row-wise methods, which add extra processing overhead per row.

This approach scales incredibly well for large datasets, and it's easy to tweak if you ever need right-shifts instead of left-shifts—just adjust the mask logic to col_indices >= (n_cols - shift_vals)!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:25:04