Getting previous row based using conditional shift

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Getting previous row based using conditional shift



I am converting an XML into a dataframe which looks something this:



enter image description here



I want the page number tag which is in the pageflag column to be copied for all the text boxes in that page which in this case would be pageflag value in row 1 copied into row 9,10 and 11



For this purpose i am using Dataframe.shift using a condition something like this:


df['pageflag'] = np.where(df['pageflag']==0,df['pageflag'].shift(1),df['pageflag'])



It should be based on this condition so as to avoid the shift for rows which already have a value like in this case it is row 13 where a new page attribute is present.



The issue i am facing is that the shift occurs only for one row after the page number tag which in my case is row 9.row 10 and 11 will still have the value zero which is not what i was expecting.



This can be achieved using a for loop as below:


for i in range(1, len(df)):
if df['pageflag'][i] == 0:
df['pageflag'][i] = df['pageflag'][i - 1]
else:
df['pageflag'][i] = df['pageflag'][i]



but this is something i want to avoid due to performance issues for large dataframes.



Any suggestions on how this can be achieved in the most efficient way?




1 Answer
1



You can use pd.Series.ffill after converting your 0 values to NaN via pd.Series.mask:


pd.Series.ffill


0


NaN


pd.Series.mask


df['pageflag'] = df['pageflag'].mask(df['pageflag'] == 0).ffill()





Same problem as i faced before with numpy.where.It does it only for the immediate row as i mentioned and not for all the rows
– abhi1489
Aug 10 at 9:37





@abhi1489, Ah I see the problem now, see update.
– jpp
Aug 10 at 9:40





Awesome!!!.No clue how that worked as i have never used mask or ffill methods.Will look into what they do.Thanks a lot for your help
– abhi1489
Aug 10 at 9:46






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS