Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas get elements until value changes

I'm working with a large dataframe with Pandas, where I need to have all the elements until one value changes. For example:

e1    e2
1     15
1     16
1     17
0     14
0     13
0     14
1     16
1     15

Here I want first three elements, then the next three and after that the last two. I was wondering if there is a method of Pandas for this.

like image 540
M Selles Avatar asked Dec 03 '25 09:12

M Selles


1 Answers

You need to do some transformations on your DataFrame in order to get the information you want.

I would do like this:

df["e3"] = df["e1"].shift(1)
df["e4"] = df["e1"] != df["e3"]
df["e5"] = df["e4"].cumsum()
df

    e1  e2  e3      e4      e5
0   1   14  NaN     True    1
1   1   15  1       False   1
2   1   15  1       False   1
3   0   16  1       True    2
4   0   1   0       False   2
5   0   15  0       False   2
6   1   15  0       True    3
7   1   16  1       False   3

See how e5 now uniquely names each group.

Now we can use a groupby function to get each group, like this:

groups = df.groupby("e5")

And perform your action on the groups

like image 108
firelynx Avatar answered Dec 06 '25 00:12

firelynx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!