I already asked my question but it was not enough accurate in its description. Smart people in this forum already proposed solutions, but I forgot(sorry) to precise that if there were zeros in the relevant columns, they should be kept.
Hello I have a dataframe like below
2014 2015 2016 2017 2018 2019
2014 10 20 30 40 0 5
2015 0 0 200 0 100 0
2016 0 0 200 140 35 10
2017 0 0 0 20 0 12
I need to have a result like this:
yearStart yearStart+1 yearStart+2 yearStart+3 yearStart+4
0 10 20 30 40 0
1 0 200 0 100 0
2 200 140 35 10 0
3 20 0 12 0 0
The idea is to select in each row, the columns between two dates:
index and index +delta,with delta a parameter (in this example 4) to put them in a dataframe.
With iterrows(), it takes too much time.
I tried with
df1 = df.apply(lambda x: pd.Series(x[x.keys()>=x.index],1)).fillna(0).astype(int)
but it doesn't work:
TypeError: ('Index(...) must be called with a collection of some kind,
1 was passed', 'occurred at index 2014')
Thank you