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?
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