2017-03-08 28 views
1

我有一個熊貓數據幀,看起來像這樣:引用到iterrows下一個索引()

 top   heading page_no 
0 000000   Intro  0 
1 100164   Summary  1 
2 100451  Experience  1 
3 200131   Awards  2 
4 200287   Skills   2 
5 300147  Education  3 
6 300273   Awards  3 
7 300329  Interests  3 
8 300434 Certifications  3 
9 401135    End  4 

我用它使用這個數據幀從其他數據框獲得內容的過濾器。它需要的頂部之間的一切過濾即從000000到100164,並以此類推,直到300434到401135.

for index,row in df_heads.iterrows(): 
    begin = int(row['top']) 
    end = ??? 
    filter_result = result['data'][(result.top < end) & (result.top > begin)] 
    print(row['heading']) 
    print(filter_result) 
    sections[row['heading']] = filter_result 
    end = begin 

我應該與使我們得到了過濾器的內容,以正確的方式進行初始化?

回答

1

我想你可以通過shift創建新列,然後在必要時更換最後NaN0fillna

df_heads['shifted_top'] = df_heads['top'].shift(-1).fillna(0) 
print (df_heads) 
     top   heading page_no shifted_top 
0  0   Intro  0  100164.0 
1 100164   Summary  1  100451.0 
2 100451  Experience  1  200131.0 
3 200131   Awards  2  200287.0 
4 200287   Skills  2  300147.0 
5 300147  Education  3  300273.0 
6 300273   Awards  3  300329.0 
7 300329  Interests  3  300434.0 
8 300434 Certifications  3  401135.0 
9 401135    End  4   0.0 

for index,row in df_heads.iterrows(): 
    begin = int(row['top']) 
    end = int(row['shifted_top']) 
    print (begin, end) 

0 100164 
100164 100451 
100451 200131 
200131 200287 
200287 300147 
300147 300273 
300273 300329 
300329 300434 
300434 401135 
401135 0 
相關問題