Reputation: 43
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.
Upvotes: 3
Views: 2491
Reputation: 32214
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
Upvotes: 11