不清楚正是你想要的,但下面演示瞭如何使用iloc
達到你要的是什麼:
In [206]:
df = pd.DataFrame({'row': np.arange(10), 'value':np.random.randn(10)})
df
Out[206]:
row value
0 0 1.183865
1 1 -0.206004
2 2 -0.251152
3 3 -0.246940
4 4 -0.898938
5 5 -0.278680
6 6 -0.658099
7 7 2.007017
8 8 -2.304950
9 9 0.599819
In [211]:
for i in range(len(df)):
print("row", df.iloc[i]['row'], "value: ", df.iloc[i]['value'])
if i != 0 and i%3 == 0:
print("prev -2 row: ", df.iloc[i-2]['row'], "value: ", df.iloc[i-2]['value'])
row 0.0 value: 1.18386492781
row 1.0 value: -0.206003776639
row 2.0 value: -0.251152226938
row 3.0 value: -0.246940111559
prev -2 row: 1.0 value: -0.206003776639
row 4.0 value: -0.898938240373
row 5.0 value: -0.278680105208
row 6.0 value: -0.658099354022
prev -2 row: 4.0 value: -0.898938240373
row 7.0 value: 2.00701698585
row 8.0 value: -2.30495039859
row 9.0 value: 0.599819374456
prev -2 row: 7.0 value: 2.00701698585
你有沒有看着['ILOC '](http://pandas.pydata.org/pandas-docs/stable/indexing.html#selection-by-position)? – EdChum 2015-02-24 14:44:11