What I wish to achieve is to be able to normalise my values so that they all start on a arbitrary day-zero. Imagine the DataFrame below:
df = pd.DataFrame(np.array([[1, 0, 0], [4, 5, 0], [7, 8, 9], [7, 8, 9], [4, 5, 0], [7, 8, 9]]),
columns=['a', 'b', 'c'],
index = ['1/1/2000', '1/1/2001', '1/1/2002', '1/1/2003', '1/1/2004', '1/1/2005'])
Which results in the following:
a b c
1/1/2000 1 0 0
1/1/2001 4 5 0
1/1/2002 7 8 9
1/1/2003 7 8 9
1/1/2004 4 5 0
1/1/2005 7 8 9
What I am trying to achieve is the find the first non-zero value in each column, and shift the values in that column to the index zero. so the final dataFrame looks similar to this:
a b c
1/1/2000 1 5 9
1/1/2001 4 8 9
1/1/2002 7 8 0
1/1/2003 7 5 9
1/1/2004 4 8 0
1/1/2005 7 0 0
I tried to use a shift, and I can use something like this:
df.b = df.b.shift(periods = -1, fill_value = 0)
but currently I do not know any pandas-friendly way to do this without using for loops, etc.
Appreciate if some can help on the best to find the first non-zero value and then shifting the values correctly to index zero.