There is a DataFrame z
with
['title','module_number','beginners','finishers']
columns. I need to shift z.beginners
Series as every last module_number
should have NaN there, and join it to DataFrame as new column z.shift
.
I've tried to do it with pd.concat
. My code:
def exp2(df):
fx = df.join(df.beginners[1:].rename('shifted').reset_index()).drop('index', axis =1)
return fx
def exp3(z,lst):
common = list()
for title in lst:
df = z[z['title'] == title]
example = exp2(df)
common.append(example)
return pd.concat(common)
exp3(z, z.title.unique())
That works fine only for the first title in z
and incorrectly for the second one. However, it doesn't work with any other title:
title module_number beginners finishers shift
0 Uu 1 518 458 434
1 Uu 2 434 406 NaN
3 Kk 1 627 563 27
4 Kk 2 521 427 NaN
5 Rr 1 158 142 NaN
6 Rr 2 136 106 NaN
Does anyone know an alternative solution or how I can enhance my code?
I've just done it the way without loop:
shifted = z.beginners.where(z.module_number != 1)[1:]
z.join(shifted.rename('shifted').reset_index()).drop('index',axis = 1)
There was also a possibility to use .shift(1) func