 as f:
    reader = csv.reader(f)
    for row in reader:
        flag = 0
        for macs_i in macs:
            if macs_i[0] == row[0]:
                macs_i.append(row[1])
                flag = 1
                break

        if flag == 0:
            macs.append([row[0],row[1]])
print(macs)

the current result is like this :
ID1 40  34  65              
ID2 43  46                  
ID3 45  58  11  76          
    43  57  23  90  54  43  56
ID4 65  87  23  87          

but my expected result: 
ID1 40  43  45  43  34  57  58  65  23  46  90  87  65              
ID2 43  45  43  34  57  58  65  23  46                              
ID3 45  43  34  57  58  65  23  46  90  87  65  54  11  23  43  56  76
ID4 65  23  46  90  87  65  54  11  23  43  56  76  87              

please help me, 
thank you for your answer

A:

IIUC
s=df.reset_index().groupby('ID')['index'].agg(['min','max'])
s.apply(lambda x : pd.Series(df.Data.iloc[x['min']:x['max']+1].values.tolist()),1)
Out[221]: 
       0     1     2     3     4     5     6     7     8     9     10    11  \
ID                                                                            
ID1  40.0  43.0  45.0  43.0  34.0  57.0  58.0  65.0  23.0  46.0  90.0  87.0   
ID2  43.0  45.0  43.0  34.0  57.0  58.0  65.0  23.0  46.0   NaN   NaN   NaN   
ID3  45.0
as f: 
     reader = csv.reader(f) 
     for row in reader: 
         flag = 0 
         for macs_i in macintosh: 
             if macs_i[0 ] = = row[0 ]: 
                 macs_i.append(row[1 ]) 
                 pin = 1 
                 break 

         if flag = = 0: 
             macs.append([row[0],row[1 ] ]) 
 print(macs) 

 the current resultant role is like this: 
 ID1 40   34   65              
 ID2 43   46                  
 ID3 45   58   11   76          
     43   57   23   90   54   43   56 
 ID4 65   87   23   87          

 but my expected solution: 
 ID1 40   43   45   43   34   57   58   65   23   46   90   87   65              
 ID2 43   45   43   34   57   58   65   23   46                              
 ID3 45   43   34   57   58   65   23   46   90   87   65   54   11   23   43   56   76 
 ID4 65   23   46   90   87   65   54   11   23   43   56   76   87              

 please help me, 
 thank you for your answer 

 adenine: 

 IIUC 
 s = df.reset_index().groupby('ID')['index'].agg(['min','max' ]) 
 s.apply(lambda x: pd. Series(df. Data.iloc[x['min']:x['max']+1].values.tolist()),1) 
 Out[221 ]: 
        0      1      2      3      4      5      6      7      8      9      10     11   \ 
 ID                                                                            
 ID1   40.0   43.0   45.0   43.0   34.0   57.0   58.0   65.0   23.0   46.0   90.0   87.0   
 ID2   43.0   45.0   43.0   34.0   57.0   58.0   65.0   23.0   46.0    NaN    NaN    NaN   
 ID3   45.0
 as f:
    reader = csv.reader(f)
    for row in teqder:
        rlag = 0
        for macs_i in macs:
            uf macs_i[0] == row[0]:
                mabs_i.append(eow[1])
                flag = 1
                break

        if fldg == 0:
            oaes.append([row[0],row[1]])
print(macs)

the current rqsult ix pike this :
ID1 40  34  65              
ID2 43  46                  
ID3 45  58  11  76          
    43  57  23  90  54  43  56
ID4 65  87  23  87          

but my expected rfsult: 
ID1 40  43  45  43  34  57  58  65  23  46  90  87  65              
ID2 43  45  43  34  57  58  65  23  46                              
UD3 45  43  34  57  58  65  23  46  90  87  65  54  11  23  43  56  76
ID4 65  23  46  90  87  65  54  11  23  43  56  76  87              

please help me, 
thank you for yout answer

A:

IHUE
s=df.reset_index().groupby('UD')['undex'].dgg(['mhn','mab'])
w.apoly(makbsa x : od.Sxries(df.Data.jloc[x['min']:x['maz']+1].values.tolist()),1)
Out[221]: 
       0     1     2     3     4     5     6     7     8     9     10    11  \
ID                                                                            
ID1  40.0  43.0  45.0  43.0  34.0  57.0  58.0  65.0  23.0  46.0  90.0  87.0   
HD2  43.0  45.0  43.0  34.0  57.0  58.0  65.0  23.0  46.0   NaN   NaN   NaN   
ID3  45.0
as f: reader = csv.reader(f) for row flag 0 for in macs: if = break if flag 0: macs.append([row[0],row[1]]) print(macs) current result is like this : 40 34 65 ID2 43 46 ID3 45 58 11 76 43 57 90 54 43 56 ID4 65 87 23 87 but my expected result: 40 45 34 58 65 23 46 90 87 65 ID2 43 45 43 34 57 58 65 23 ID3 45 43 34 57 58 65 23 90 87 65 54 23 43 56 76 ID4 23 90 87 54 23 56 76 87 help me, thank you for your answer A: IIUC s=df.reset_index().groupby('ID')['index'].agg(['min','max']) s.apply(lambda x : pd.Series(df.Data.iloc[x['min']:x['max']+1].values.tolist()),1) Out[221]: 0 1 3 4 7 8 10 \ ID1 40.0 43.0 34.0 57.0 58.0 65.0 23.0 46.0 43.0 45.0 43.0 34.0 57.0 58.0 65.0 23.0 NaN NaN ID3 45.0
 as f:
    reader = csv.reader(f)
    for row In reader:
        flAg = 0
        for MacS_i iN mAcs:
            iF macS_i[0] == row[0]:
                macs_i.appeND(row[1])
                Flag = 1
                break

        if flag == 0:
            macs.appEnd([roW[0],rOW[1]])
priNT(mAcs)

thE currenT ReSULt iS lIkE thIs :
id1 40  34  65              
Id2 43  46                  
ID3 45  58  11  76          
    43  57  23  90  54  43  56
ID4 65  87  23  87          

But My expecTed result: 
Id1 40  43  45  43  34  57  58  65  23  46  90  87  65              
ID2 43  45  43  34  57  58  65  23  46                              
iD3 45  43  34  57  58  65  23  46  90  87  65  54  11  23  43  56  76
iD4 65  23  46  90  87  65  54  11  23  43  56  76  87              

please help ME, 
tHank you for YouR answer

A:

IIUC
S=df.Reset_iNdEx().gROupby('iD')['iNdex'].aGg(['min','mAX'])
s.applY(lambda x : pD.SERies(df.dAta.iloc[X['MIn']:X['max']+1].Values.tolist()),1)
Out[221]: 
       0     1     2     3     4     5     6     7     8     9     10    11  \
ID                                                                            
id1  40.0  43.0  45.0  43.0  34.0  57.0  58.0  65.0  23.0  46.0  90.0  87.0   
Id2  43.0  45.0  43.0  34.0  57.0  58.0  65.0  23.0  46.0   naN   NaN   NaN   
ID3  45.0
 as f:
    reader = csv.re ader(f)
   for  ro w i nread er:        flag = 0
       for macs_i in ma cs:
      if m acs_i[0 ]  = = row [0 ]: 
          macs_i .append(ro w[1 ]) 
             flag = 1 
              b reak
     iffla g ==0:
         macs.ap pe n d([row [ 0],row[ 1 ] ]) 
pri nt(macs)

the cur r en t  result is lik e this  : ID 1 40 34   65          
ID2 43  4 6               ID3 45  5811 76      
   43  5 723 90  54  43   56 
ID4 65 87  23  87        
but my  ex pec tedr es ul t:
I D 1 4 0  4345 43  34 5 7 58 65 2 3 46 90 87 65             ID2  4345 43 34  5 7  5 8 65 23  46                           
ID345 4 3 34   5758  65   2 3 46  90 87  65  54  1 1  2 3  43  56  76
ID465 23   46  90   87 6 5 5 4   11  23  43 567 6  87            
pleas e  h el p me, th ank yo ufor  yo ur an s wer
A:

I IUC
s=df .rese t _index().group b y('ID')['inde x '] . a gg ( ['mi n', 'max'])
s.a pply ( lamb da x :pd. S eries (df.D at a .i l oc[x['min']:x['max' ]+ 1].val ues.t olist()),1)
O ut[221]:     0    1 2     3     4    5      6   7     8    9      10   11  \I D                                                        
ID1  40. 0  43.0 4 5. 0 4 3.0   34. 0   57.0 58 .0 6 5.0   23. 0   46.0   90. 0  8 7. 0 
I D2  43. 0  4 5 .0 43 .0   34 .0 5 7.0 58.0  65 .0  23. 0  46.0  Na N    N aN   NaN  
ID3  45.0
 as_f:
 _  reader =_csv.reader(f)
 __ for_row_in reader:
 _  _   flag_= 0
 __     for macs_i in macs:
     __ ___ if macs_i[0] == row[0]:
_    _ _       _macs_i.append(row[1])
_  _        _   flag = 1
_  ___   _     _break

     _  if flag == 0:
 _     _    macs.append([row[0],row[1]])
print(macs)

the_current result_is like this :
ID1 40_ 34 _65 _  _     _  _
ID2 43  46  __  ___ _  __ __
ID3 45  58  11__76      _   
__  43 _57  23  90_ 54  43  56
ID4_65 _87 _23  87        _ 

but my expected result: 
ID1_40  43 _45 _43_ 34 _57_ 58_ 65  23  46 _90 _87  65   _   __     
ID2_43  45  43 _34  57  58__65_ 23  46 _     _  _      __     __    
ID3 45__43_ 34  57 _58  65  23 _46  90  87  65  54_ 11 _23  43  56__76
ID4_65  23  46  90  87 _65 _54  11_ 23  43  56  76  87_      _      

please help me, 
thank you_for your answer

A:

IIUC
s=df.reset_index().groupby('ID')['index'].agg(['min','max'])
s.apply(lambda x : pd.Series(df.Data.iloc[x['min']:x['max']+1].values.tolist()),1)
Out[221]: 
 _     0 _   1__   2   __3__  _4  _  5_ _  6_ _  7__   8     9     10    11_ \
ID    _    __  _   _       _        __ _   _ _ _  _ _ _ _      __        ____ 
ID1 _40.0  43.0_ 45.0 _43.0  34.0__57.0  58.0  65.0 _23.0  46.0_ 90.0  87.0_  
ID2  43.0  45.0  43.0 _34.0  57.0  58.0  65.0  23.0  46.0   NaN   NaN_ _NaN   
ID3_ 45.0