• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

排序数据框后更新索引

用户头像
it1352
帮助1

问题说明

采用以下数据框:

x = np.tile(np.arange(3),3)
y = np.repeat(np.arange(3),3)
df = pd.DataFrame({"x": x, "y": y})
   x  y
0  0  0
1  1  0
2  2  0
3  0  1
4  1  1
5  2  1
6  0  2
7  1  2
8  2  2

我需要先按x对其进行排序,然后仅按y对其进行排序:

I need to sort it by x first, and only second by y:

df2 = df.sort(["x", "y"])
   x  y
0  0  0
3  0  1
6  0  2
1  1  0
4  1  1
7  1  2
2  2  0
5  2  1
8  2  2

如何更改索引,使其再次上升. IE.我怎么得到这个:

How can I change the index such that it is ascending again. I.e. how do I get this:

   x  y
0  0  0
1  0  1
2  0  2
3  1  0
4  1  1
5  1  2
6  2  0
7  2  1
8  2  2

我尝试了以下方法.不幸的是,它根本不会改变索引:

I have tried the following. Unfortunately, it doesn't change the index at all:

df2.reindex(np.arange(len(df2.index)))

正确答案

#1

您可以使用查看全文

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfiafh
系列文章
更多 icon
同类精品
更多 icon
继续加载