2

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)

Amin Noor
  • 75
  • 10
  • 1
    `s` is a series index by `df.columns`, which is `['a','b','c']` in this case. When you do `df.apply` the lambda function take `x` as individual columns of `df`. Each column, in turns, is a series with the column name as its name. So each column is shifted up by the number of starting `0` in it. – Quang Hoang Mar 13 '20 at 01:17
  • Thanks, is this (-s[x.name]) work because the index name in s is the same as column name in df? – Amin Noor Mar 13 '20 at 01:45
  • Yes, more precisely, it works whenever `s.index` is a super set of `df.columns`. However, in this case they are identical by the way `s` is defined. – Quang Hoang Mar 13 '20 at 01:46
  • Thanks, If I expand this line: x.shift(periods = -s[x.name] it becomes something similar to the below: df.shift(periods = -s['a']) and from what I can see it is not a valid argument for shift, or am I missing something here? – Amin Noor Mar 13 '20 at 03:07
  • 1
    works just fine on my end. `s['a']` is `0` in this case. – Quang Hoang Mar 13 '20 at 03:12
  • You are right, thanks, my bad. I got myself confused, it is much more clear now what is going on, appreciate your input. – Amin Noor Mar 13 '20 at 03:15

0 Answers0