I have a df with annual data in a number of cols. I have code to pull the last value in each column, excluding 0s. I'd like to pull the nth row previous to the row I pulled, the issue being that the index isn't the same for all of these values. I'd like to use the n from df_periods on each col to pull the nth value previous. I'd thought of trying to index the values I get from endval but because they're not from the same row it doesn't seem to work. Any thoughts on this?
df=
col1 col2 col3 col4 col5
8 9 6 7 1
8 9 6 7 1
8 9 6 7 1
8 9 6 7 1
8 9 6 7 NaN
8 NaN 6 7 NaN
8 NaN 6 7 NaN
8 NaN 6 7 NaN
df_periods=
col1 col2 col3 col4 col5
4 3 5 4 4
endval = df.stack().groupby(level=1).last()