紫衣仙女
永远不要使用这种构造:for i in range(nrows): do_stuff(df[column][i])这是低效的。您几乎从不希望在数据帧上使用for循环,但是如果必须的话,请使用pd.Dataframe.itertuples:>>> df = pd.DataFrame({'a':[1,2,3],'b':[3,4,5]})>>> for row in df.itertuples():... print("the index", row.Index)... print("sum of row", row.a + row.b)...the index 0sum of row 4the index 1sum of row 6the index 2sum of row 8请注意,现在索引是否更改无关紧要:>>> df = df.iloc[[2,0,1]]>>> df a b2 3 50 1 31 2 4>>> for row in df.itertuples():... print("the index", row.Index)... print("sum of row", row.a + row.b)...the index 2sum of row 8the index 0sum of row 4the index 1sum of row 6最后,假设您总是可以重设索引:>>> df.drop(0, axis=0, inplace=True)>>> df a b2 3 51 2 4现在,只需使用:>>> df.reset_index() index a b0 2 3 51 1 2 4并使用drop参数不将旧索引包括在列中:>>> df.reset_index(drop=True) a b0 3 51 2 4