简体   繁体   English

在Python中使用滞后值填充数据框

[英]Filling dataframe with lagged values in Python

I am trying to write a loop which fills the elements in a dataframe or matrix with the values of the previous year. 我正在尝试编写一个循环,使用上一年的值填充数据框或矩阵中的元素。 The columns represent different years within the 50 year horizon. 这些列代表50年内的不同年份。 The rows represent different discrete ages (up to 50 years old). 这些行代表不同的离散年龄(最长50岁)。 The initial distribution in year 1 (green vector) is given. 给出了第一年的初始分布(绿色矢量)。 I would like to successively move the elements through the df or matrix. 我想先后通过df或矩阵移动元素。 Hence, element 1,1 depicts the surface of age 1 in year 1. As a consequence, that element moves to 2,2; 因此,元素1,1描绘了第一年的年龄1的表面。结果,该元素移动到了2,2; 3,3 and so on. 3,3等等。 The last row should move to the first row in the next year (indicated by the blue arrow). 最后一行应移至下一年的第一行(由蓝色箭头指示)。

在此处输入图片说明

I have tried to iterate through the dataframe, but I think the Keyerror has to do with the fact that [index-1] has to be bound? 我试图遍历数据 ,但是我认为Keyerror与必须绑定[index-1]的事实有关吗?

import numpy as np
import pandas as pd

years = np.arange(50)
a_vector = np.arange(50)
pop_matrix = pd.DataFrame(0, index=a_vector, columns=years)

#Initial vector (green)
A0 = 5000000
for a, rows in pop_matrix.iterrows():
    pop_matrix[0][a] = A0 / len(pop_matrix)

#Incorrect attempt
for t in years:
    for a, rows in pop_matrix.iterrows():
        if t-1 >= 0 and a-1 >= 0:
            pop_matrix[t][a] = pop_matrix[t-1][a-1]

I think the best way is to use numpy roll function. 我认为最好的方法是使用numpy roll函数。

Extract the values of your index and then apply numpy roll each time with a different shift. 提取索引值,然后每次以不同的班次应用numpy roll。 Example : 范例:

for year in years:
    col = pop_matrix.columns.tolist()[year]
    pop_matrix[col] = numpy.roll(a_vector, shift=year+1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM