Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add a column with sequence values if conditions of value in another column with binary values is satisfied

I have a dataframe df with column A with random numbers and column B with categories. Now, I obtain another column C using the code below:

df.loc[df['A'] >= 50, 'C'] = 1 
df.loc[df['A'] < 50, 'C'] = 0 

I want to obtain a column ‘D’ which creates a sequence if 1 is encountered else returns the value 0. The required dataframe is given below.

Required df

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

A   B   C   D
17  a   0   0
88  a   1   1
99  a   1   2
76  a   1   3
73  a   1   4
23  b   0   0
36  b   0   0
47  b   0   0
74  b   1   1
80  c   1   1
77  c   1   2
97  d   1   1
30  d   0   0
80  d   1   2

>Solution :

Use GroupBy.cumcount with Series.mask:

df['D'] = df.groupby(['B', 'C']).cumcount().add(1).mask(df['C'].eq(0), 0)
print (df)
A   B   C   D
17  a   0   0
88  a   1   1
99  a   1   2
76  a   1   3
73  a   1   4
23  b   0   0
36  b   0   0
47  b   0   0
74  b   1   1
80  c   1   1
77  c   1   2
97  d   1   1
30  d   0   0
80  d   1   2

Or numpy.where:

df['D'] = np.where(df['C'].eq(0), 0, df.groupby(['B', 'C']).cumcount().add(1))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading