How to loop through each row in pandas dataframe and set values equal to nan after a threshold is surpassed?
Let's try this:
df.where(df.where(df > 7).bfill(axis=1).notna())
Output:
0 1 2 3 4 5A 5 5 10 9 NaN NaNB 10 10 10 8 NaN NaNC 8 8 0 9 NaN NaND 10 10 11 4 2.0 9.0E 0 9 1 5 8.0 NaN
create a mask m
by using df.where
on df.gt(7)
and bfill
and isna
. Finally, indexing df
using m
m = df.where(df.gt(7)).bfill(1).notna()df[m]Out[24]: 0 1 2 3 4 5A 5 5 10 9 NaN NaNB 10 10 10 8 NaN NaNC 8 8 0 9 NaN NaND 10 10 11 4 2.0 9.0E 0 9 1 5 8.0 NaN
A very nice question , reverse the order then cumsum
the one equal to 0 should be NaN
df.where(df.iloc[:,::-1].gt(7).cumsum(1).ne(0)) 0 1 2 3 4 5A 5 5 10 9 NaN NaNB 10 10 10 8 NaN NaNC 8 8 0 9 NaN NaND 10 10 11 4 2.0 9.0E 0 9 1 5 8.0 NaN