0

This may be a very special conversion, but assuming I have a panadas DataFrame df:

import pandas as pd 
df=pd.DataFrame({'a':[1,2,3,4,5,6,7,8,9],'b':[2,2,2,2,3,3,3,3,3]}

   a  b
0  1  2
1  2  2
2  3  2
3  4  2
4  5  3
5  6  3
6  7  3
7  8  3
8  9  3

I want to group it based on column "b", then cut column "a" to subsets with a certain number of consecutive elements and all possibilities within each group, to form a 2D array (DataFrame or numpy array). For example if I want 3-consecutive-element subsets, the results should be:

1  2  5  6  7
2  3  6  7  8
3  4  7  8  9

I'm using the following codes:

gp=df.groupby(['b'])
n=3
comb=[]
for b, frame in gp: 
    for j in range(len(frame)-n+1):
        ser=frame.a.iloc[j:j+n]
        comb.appen(ser)

But it doesn't look like a python way. Is there any better way to do that? Thanks!

John
  • 41
  • 1
  • 6

0 Answers0