Search code examples
pandasdataframegroup-by

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.

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()