Skip to content Skip to sidebar Skip to footer

Move The Value In The Second Duplicate To The First Duplicate

This post is to assign value of last row to first row: Move last value to first value. I would like to move the value in the second duplicate to the first duplicate and set others

Solution 1:

Let us do reindex with apply and select the second of row , then do the same as we did from pervious question

df['New']=df.groupby('DateOutBed')['OutBedTime'].apply(lambda x : x.iloc[[1]]).reset_index(level=1,drop=True).reindex(df.DateOutBed).values
df['New']=df.New.mask(df.DateOutBed.duplicated())
df
   ID      OutBedTime  DateOutBed             New
0116/05/20180:1716/05/201816/05/20184:051116/05/20184:0516/05/2018             NaN
2116/05/20186:0516/05/2018             NaN
3117/05/20181:2717/05/201817/05/20184:414117/05/20184:4117/05/2018             NaN
5117/05/20185:3217/05/2018             NaN

Check the update

df['New']=df.groupby('DateOutBed')['OutBedTime'].transform(lambda x : x.iloc[1] if len(x)>1 else x.iloc[0])
df['New']=df.New.mask(df.DateOutBed.duplicated())

Post a Comment for "Move The Value In The Second Duplicate To The First Duplicate"