至尊宝的传说
您需要在此处使用df.shiftdf.shift(i)将整个数据帧下移i个单位。所以对于我= 1输入: x1 x2 0 206 214 1 226 234 2 245 253 3 265 272 4 283 291输出: x1 x20 Nan Nan 1 206 214 2 226 234 3 245 253 4 265 272 因此,运行此脚本以获取预期的输出import pandas as pddf = pd.DataFrame({'x1': ['206', '226', '245',' 265', '283'], 'x2': ['214', '234', '253', '272', '291']})print(df)df['x2'] = df['x2'].shift(1)print(df)