Aaron Horowitz
Aaron Horowitz

Reputation: 63

Pandas Dataframe ffill with Previous Nonzero value in groups

Asked a very similar question earlier that got a good answer, but realized that there's more to it. I have a dataframe like:

      LOT_ID           STEP_NUMBER
0     4258556          0.0
1     4258556          0.0
2     4258556          1.0
3     4258556          1.0
4     4258556          2.0
5     4258556          2.0
6     4258556          3.0
7     4258556          3.0
8     4258556          4.0
9     4258556          5.0
10    4258556          5.0
11    4258556          6.0
12    4258556          6.0
13    4258556          7.0
14    4258556          7.0
15    4258556          0.0
16    4261512          0.0
17    4261512          1.0
18    4261512          1.0
19    4261512          2.0
20    4261512          2.0
21    4261512          3.0
22    4261512          3.0
23    4261512          4.0
24    4261512          5.0
25    4261512          5.0
26    4261512          6.0
27    4261512          6.0
28    4261512          7.0
29    4261512          7.0
30    4261512          0.0
31    4265282          0.0
32    4265282          1.0
33    4265282          1.0
34    4265282          2.0
35    4265282          2.0
36    4265282          3.0
37    4265282          3.0
38    4265282          4.0
39    4265282          4.0
40    4265282          5.0
41    4265282          5.0
42    4265282          6.0
43    4265282          7.0
44    4265282          7.0
45    4265282          0.0
46    4265706          0.0
47    4265706          1.0
48    4265706          1.0

I want to ffill the trailing zeros in step_number for each lot_id group with one greater than the previous, non-zero value to give:

    LOT_ID           STEP_NUMBER
0     4258556          0.0
1     4258556          0.0
2     4258556          1.0
3     4258556          1.0
4     4258556          2.0
5     4258556          2.0
6     4258556          3.0
7     4258556          3.0
8     4258556          4.0
9     4258556          5.0
10    4258556          5.0
11    4258556          6.0
12    4258556          6.0
13    4258556          7.0
14    4258556          7.0
15    4258556          8.0
16    4261512          0.0
17    4261512          1.0
18    4261512          1.0
19    4261512          2.0
20    4261512          2.0
21    4261512          3.0
22    4261512          3.0
23    4261512          4.0
24    4261512          5.0
25    4261512          5.0
26    4261512          6.0
27    4261512          6.0
28    4261512          7.0
29    4261512          7.0
30    4261512          8.0
31    4265282          0.0
32    4265282          1.0
33    4265282          1.0
34    4265282          2.0
35    4265282          2.0
36    4265282          3.0
37    4265282          3.0
38    4265282          4.0
39    4265282          4.0
40    4265282          5.0
41    4265282          5.0
42    4265282          6.0
43    4265282          7.0
44    4265282          7.0
45    4265282          8.0
46    4265706          0.0
47    4265706          1.0
48    4265706          1.0

How can I do this? The question that I posed before assumed it was only trailing zeros in a column that would need to be replaced. But there are a number of groups with trailing zeros that require replacement.

Upvotes: 0

Views: 26

Answers (0)

Related Questions