fill each row of a dataframe with zero starting from the column in which the values decreases with respect to the value in the previous column

Question:

I would like to fill each row of a dataframe with zero starting from the column in which the value decreases with respect to the value in the previous column

I have the following Dataframe:

ActualDf = pd.DataFrame(np.array([[2, 3, 2, 3, 4, 5, 0, 0, 0, 0, 0], [1, 1, 1, 2, 2, 3, 1, 1, 0, 0, 0], [2, 2, 2, 3, 3, 5, 3, 3, 0, 0, 0], [2, 3, 2, 3, 3, 3, 4, 1, 2, 0, 0], [3, 3, 1, 2, 2, 5, 0, 0, 0, 0, 0], [1, 2, 3, 1, 2, 3, 0, 0, 0, 0, 0]]),
columns=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

)

And I would like to obtain this one

DesiredDf = pd.DataFrame(np.array([[2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 2, 2, 3, 0, 0, 0, 0, 0], [2, 2, 2, 3, 3, 5, 0, 0, 0, 0, 0], [2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0], [3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0]]),
columns=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'])

How is it possible to da that in an efficient way?

Asked By: Pachita

||

Answers:

You could use diff and lt to find where the difference is negative, and then use cumsum and gt to flag all cells after the first negative value.

ActualDf[ActualDf.diff(axis=1).lt(0).cumsum(1).gt(0)] = 0
Answered By: Chris

Here is an approach similar using a mask:

DesiredDf =ActualDf.mask(ActualDf.diff(axis=1).lt(0).cummax(axis=1), 0)

output:

   0  1  2  3  4  5  6  7  8  9  10
0  2  3  0  0  0  0  0  0  0  0   0
1  1  1  1  2  2  3  0  0  0  0   0
2  2  2  2  3  3  5  0  0  0  0   0
3  2  3  0  0  0  0  0  0  0  0   0
4  3  3  0  0  0  0  0  0  0  0   0
5  1  2  3  0  0  0  0  0  0  0   0
Answered By: mozway
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.