1

I have Input DataFrame given below. For every first unique element row ofID it has to be written zero in the Output DataFrame Zeros_For_UniqueID column. After then the consecutive zeros should be counted from theCount variable before the integer appear for the unique IDand have to be put in output column Zeros_For_UniqueID.

Input DataFrame:

ID  Count
1234    1
1234    2
1234    0
1234    0
1234    0
1234    1
1234    1
5678    1
5678    5
5678    4
5678    0
1111    0
1111    0
1111    1
1111    2
1111    0
1111    0
1111    2

Output DataFrame

ID  Count   Zeros_For_UniqueID
1234    1   0
1234    2   0
1234    0   0
1234    0   1
1234    0   2
1234    1   3
1234    1   0
5678    1   0
5678    5   0
5678    4   0
5678    0   0
1111    0   0
1111    0   1
1111    1   2
1111    2   0
1111    0   0
1111    0   1
1111    2   2

Can anyone help me in solving this. I am new to the python and trying to solve this for my further research.Thanks!

1 Answer 1

1

I am not sure the speed, but this will achieve what you need , you need groupby+cumsum+shift

df['New']=df.groupby('ID').\
     apply(lambda x : x.groupby(x.Count.ne(0).cumsum().shift().fillna(False)).cumcount()).\
       sort_index(level=1).values
df
Out[323]: 
      ID  Count  New
0   1234      1    0
1   1234      2    0
2   1234      0    0
3   1234      0    1
4   1234      0    2
5   1234      1    3
6   1234      1    0
7   5678      1    0
8   5678      5    0
9   5678      4    0
10  5678      0    0
11  1111      0    0
12  1111      0    1
13  1111      1    2
14  1111      2    0
15  1111      0    0
16  1111      0    1
17  1111      2    2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.