为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

偶然的你 2021-04-02 14:11:20
我已经定义了一个数据集:df=pd.DataFrame(list(xx))然后,我根据性别过滤了一些数据。df=df[df["sex"]=="1"]那么我应该遍历所有数据。row,col=df.shapefor i in range(row):    print(df["name"][i])  # error我调试代码,发现“ df”行索引是旧索引,因为删除了许多不合格的数据。例如df [“ sex”] [1] == 1被删除,因此循环除外。非常感谢如何对DataFrame行序列进行重新排序!
查看完整描述

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


查看完整回答
反对 回复 2021-04-27
  • 1 回答
  • 0 关注
  • 965 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号