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

Get the first non-zero element in groupby pandas

I have created the following pandas dataframe:

import pandas as pd

ds = {'cycle' : [1,1,1,1,2,2,2,2], 'values' : [0,0,-1,1,2,0,0,1]}

df = pd.DataFrame(data=ds)

The dataframe looks like this:

df

  cycle values
0   1   0
1   1   0
2   1   -1
3   1   1
4   2   2
5   2   0
6   2   0
7   2   1

I need to create a new dataframe (called ds) which contains only the first record with non-zero value for the columns values for each cycle.

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

I guess that it can be done with something around the df.groupby(['cycle']) code.

So the resulting dataframe would look like this:

   cycle    values
0   1        -1
1   2         2

>Solution :

You can

  • filter out the zero elements

  • use groupby to group by cycle

  • extract the first element for each group

    df[df['values'] != 0].groupby(['cycle']).first()
    
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