1 回答

TA贡献1839条经验 获得超15个赞
永远不要使用这种构造:
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 0
sum of row 4
the index 1
sum of row 6
the index 2
sum of row 8
请注意,现在索引是否更改无关紧要:
>>> df = df.iloc[[2,0,1]]
>>> df
a b
2 3 5
0 1 3
1 2 4
>>> for row in df.itertuples():
... print("the index", row.Index)
... print("sum of row", row.a + row.b)
...
the index 2
sum of row 8
the index 0
sum of row 4
the index 1
sum of row 6
最后,假设您总是可以重设索引:
>>> df.drop(0, axis=0, inplace=True)
>>> df
a b
2 3 5
1 2 4
现在,只需使用:
>>> df.reset_index()
index a b
0 2 3 5
1 1 2 4
并使用drop参数不将旧索引包括在列中:
>>> df.reset_index(drop=True)
a b
0 3 5
1 2 4
添加回答
举报