如何对DataFrame行序列进行重新排序

我已经定义了一个数据集:


df=pd.DataFrame(list(xx))

然后,我根据性别过滤了一些数据。


df=df[df["sex"]=="1"]

那么我应该遍历所有数据。


row,col=df.shape

for i in range(row):

    print(df["name"][i])  # error

我调试代码,发现“ df”行索引是旧索引,因为删除了许多不合格的数据。例如df [“ sex”] [1] == 1被删除,因此循环除外。


非常感谢如何对DataFrame行序列进行重新排序!


偶然的你
浏览 966回答 1
1回答

紫衣仙女

永远不要使用这种构造: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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python