如何从 pandas 数据帧生成向量?

问题描述 投票:0回答:1

下面是

csv
文件的屏幕截图。我想使用 pandas 生成增长率向量。

1

在我的例子中,增长率定义为

log(this year/previous year)

python pandas dataframe quantitative-finance
1个回答
0
投票

这应该有效:

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,然后将原始列除以移位后的列。

© www.soinside.com 2019 - 2024. All rights reserved.