2017-10-12 11 views
0

4行のセクションにスライスする必要があります。ここでは、最初のデータフレームの行数が4であり、第二のグループ4の数は2pandasのn:n + k行ごとにグループ化します。最後の行+剰余

Go  Per Votes  group 
NaN 40.726126 NaN   3 
NaN 40.727271 36.0   3 
NaN 40.719560 NaN   3 
NaN 40.729198 19.0   3 
NaN 40.726126 NaN   4 
NaN 40.727271 36.0   4 
NaN 40.719560 NaN   4 
NaN 40.729198 19.0   4 
NaN 40.726126 NaN   4 
NaN 40.727271 36.0   4 
NaN 40.719560 NaN   5 
NaN 40.729198 19.0   5 

であることはここで何を2グループ4Sは、ありますグループ3は、4グループ3は、一つのグループがありますされて見ます私は私の出力は、私はグループ4に到達したとき、それが唯一それが最後のグループは/またはグループ間で(私のリストがどのくらい依存)と言うにもかかわらず、最初のグループを印刷している

for i in unique_group: 
    this_group = df_group[df_group['group'] == i] 
    count_items = this_group.shape[0] 
    if count_items > 4: 
      remainder = count_items % 4 
      divide = int(count_items/4) 
      repeat_group = divide 
     else: 
      repeat_group = 1 
    for repeat in range(1, repeat_group+1): 
     if count_items > 4: 
      if repeat==repeat_group: 
       this_group = this_group.iloc[:repeat*4+remainder,:] 
       print "last group" 
      elif repeat == 1: 
       this_group = this_group.iloc[:repeat*4,:] 
       print "first group" 
      else: 
       this_group = this_group.iloc[(repeat-1)*4+1:repeat*4,:]   
       print "between group" 
     print this_group 

持っています。

+0

私はグループ4の6行を見ませんでした2 – Wen

+0

yea cuz 2番目のグループは残りの2つで、グループ4の2つのグループです。グループ1は4、グループ2は2です。 :/ – JamAndJammies

答えて

0

データフレームがdfと仮定しています。

def chunker(seq, size): 
    return (seq[pos:pos + size] for pos in range(0, len(seq), size)) 

grouped = df.groupby('group') 
groups = [] 
for _, gr in grouped: 
    for chunk in chunker(gr, 4): 
     groups.append(chunk) 

for gr in groups: 
    print(len(gr)) 

これは、すべてのチャンクグループを含むリストを作成します。

関連する問題