oppressionslayer
oppressionslayer

Reputation: 7204

How do you make combined rolling groups in Pandas

How do you get rolling groups in Pandas I need the following group (1,2), then group (2,3), then group (3,4), etc. The best i can do is group (1,2), then group (3,4). I take group 1, add the values to group 2. Then the next iteration is group (2,3). I take group 2's newly updated values, and add them to group 3's original values. I then take those group 3 newly updated values, and add them to group 4's original values, so we get:

The most important parts of this, don't get stuck on adding the values in the right order, really the most important thing is that i want to update a group, i want to update group 2, with group 1's values (my post is just an example) , then in the next transform, i want those new group values i updated in group2 to update the next group, which is 3. Then in the next transform or apply, I want those new group 3 values so i can update group4. I hope that makes sense

num group
1       1
2       1
2       1
4       1
5       2
6       2
7       2
8       2
9       3
10      3
11      3
12      3
13      4
14      4
15      4
16      4
df=pd.read_clipboard()

I want my first group to be the following. group two has had it's values added by group 1:

1       1
2       1
3       1
4       1
6       2
8       2
10      2
14      2

My second group will then hopefully be the new modified values due to adding group one to them. group 3 will have it's original values added by group 2's new values:

6       2
8       2
10      2
14      2
15      3
18      3
21      3
26      3

My third group to be group 3's new values. and group four will be it's original values added by group 3 in order:

15      3
18      3
21      3
26      3
29      4
33      4
36      4
42      4

I tried

df.groupby(np.arrange(len(df))//4))

, except it only splits it by groups (1,2) the then the next group is (3,4). I need (1,2), (2,3), (3,4). This is due to me processing group 1 to make group 2's values. I then use group 2 to create group 3's values. I then use group 3 to make group 4's values. Any help on this would be appreciated. I made a simple example because I don't need help with what I'm doing with the groups, I just need to know how to group like that.

Again, this is just an example, The most important parts of this, don't get stuck on adding the values in the right order, i'm not trying to test anyone. really the most important thing is that i want to update a group, i want to update group 2, with group 1's values (my post is just an example) , then in the next transform, i want those new group values i updated in group2 to update the next group, which is 3. Then in the next transform or apply, I want those new group 3 values so i can update group4. I hope that makes sense

Upvotes: 2

Views: 99

Answers (3)

Georgina Skibinski
Georgina Skibinski

Reputation: 13377

Prerequisites:

df["group_sub"]=df.groupby("group").cumcount()
dfprev=df["num"]
for i in range(1, df.group.nunique()):
    dfprev+=df["num"].groupby(df["group_sub"]).shift(i).fillna(0) 
df.drop("group_sub", axis=1, inplace=True)

You can do:

df_series=[df.loc[df.group.isin(df.group.unique()[i:i+2])] for i in range(df.group.nunique()-1)]

Upvotes: 2

BENY
BENY

Reputation: 323226

I will do

s=df.group.drop_duplicates()
l=[df.loc[df.group.isin([x,y])]for x , y in zip(s.iloc[1:],s.shift().iloc[1:])]

Update

df['num']=df['num'].groupby(df.groupby('group').cumcount()).cumsum()
s=df.group.drop_duplicates()
l=[df.loc[df.group.isin([x,y])]for x , y in zip(s.iloc[1:],s.shift().iloc[1:])]
l[0]
   num  group
0    1      1
1    2      1
2    2      1
3    4      1
4    6      2
5    8      2
6    9      2
7   12      2

Upvotes: 3

Umar.H
Umar.H

Reputation: 23099

df['grp'] = df.apply(lambda x : x.iloc[::4]).groupby('num').cumsum()
df['grp'] = df['grp'].ffill()
print(df)
    num  group  grp
0     1      1  1.0
1     2      1  1.0
2     3      1  1.0
3     4      1  1.0
4     5      2  2.0
5     6      2  2.0
6     7      2  2.0
7     8      2  2.0
8     9      3  3.0
9    10      3  3.0
10   11      3  3.0
11   12      3  3.0
12   13      4  4.0
13   14      4  4.0
14   15      4  4.0
15   16      4  4.0

Upvotes: 2

Related Questions