以下是一种方法:
import pandas as pd
# read CSV and parse dates
df = pd.read_csv('tmp.csv', delim_whitespace=True, names=range(5),
parse_dates={'date': [0, 1]})
# find indices of shifted values
n = 1
shifted = df['date'] + pd.Timedelta(n, 's')
indices = df['date'].searchsorted(shifted)
# add a column with the shift
df['shift'] = df[2].reindex(indices).reset_index(drop=True) - df[2]
print(df)
结果是:
date 2 3 4 shift
0 2017-11-05 09:20:01.134 2123.0 12.23 34.12 300.0
1 2017-11-05 09:20:01.789 2133.0 32.43 45.62 330.0
2 2017-11-05 09:20:02.238 2423.0 35.43 55.62 NaN
3 2017-11-05 09:20:02.567 3423.0 65.43 56.62 NaN
4 2017-11-05 09:20:02.948 2463.0 45.43 58.62 NaN