这应该有效:
import numpy as np
import pandas as pd
np.random.seed(42)
YearCode = np.arange(1970, 1975)
df = pd.DataFrame(np.random.rand(5, 3), columns = ['VariableCode', 'Region', 'AggValue'])
df['YearCode'] = YearCode
shifted = df['YearCode'].shift(1)
df['logValue'] = df['YearCode']/shifted
df['logValue'] = np.log(df['logValue'])
print(df)
VariableCode Region AggValue YearCode logValue
0 0.374540 0.950714 0.731994 1970 NaN
1 0.598658 0.156019 0.155995 1971 0.000507
2 0.058084 0.866176 0.601115 1972 0.000507
3 0.708073 0.020584 0.969910 1973 0.000507
4 0.832443 0.212339 0.181825 1974 0.000507
工作原理:
本质上,您将“YearCode”移位 1,然后将原始列除以移位后的列。