I am confused about the use of apply and x.name in the following example:
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'])
s = df.ne(0).cumsum().eq(0).sum()
df = df.apply(lambda x: x.shift(periods = -s[x.name], fill_value = 0))
The final df has normalised values, based on the discussion here.
I understand the difference between map, apply and applymap. Also I read the x.name explanation here.
However, I am still confused about two things:
why in scenarios like this, we use apply rather than applymap, since we are getting element-wise operation outcome from what I understand.
Secondly and more importantly, how x.name works here. Can someone please expand the lambda through an exapnded function. If I wanted to write that line as a for loop and function how it would have been look like. (x.shift and x.name does not make sense, It seems one x refer to a dataframe while the other refers to another, etc)