Home > Enterprise >  fill each row of a dataframe with zero starting from the column in which the values decreases with r
fill each row of a dataframe with zero starting from the column in which the values decreases with r

Time:08-08

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?

CodePudding user response:

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

CodePudding user response:

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
  • Related