Pandas:重新索引未排序数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18878308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:09:55  来源:igfitidea点击:

Pandas: Reindex Unsorts Dataframe

pythonsortingpandasreindex

提问by David Yang

I'm having some trouble sorting and then resetting my Index in Pandas:

我在排序和重置 Pandas 中的索引时遇到了一些麻烦:

dfm = dfm.sort(['delt'],ascending=False)
dfm = dfm.reindex(index=range(1,len(dfm)))

The dataframe returns unsorted after I reindex. My ultimate goal is to have a sorted dataframe with index numbers from 1 --> len(dfm) so if there's a better way to do that, I wouldn't mind,

重新索引后,数据框返回未排序。我的最终目标是拥有一个索引号从 1 --> len(dfm) 排序的数据框,所以如果有更好的方法来做到这一点,我不介意,

Thanks!

谢谢!

回答by Ryan Saxe

Instead of reindexing, just change the actual index:

而不是重新索引,只需更改实际索引:

dfm.index = range(1,len(dfm) + 1)

Then that wont change the order, just the index

然后那不会改变顺序,只是索引

回答by Phillip Cloud

I think you're misunderstanding what reindexdoes. It uses the passed index to select values along the axis passed, then fills with NaNwherever your passed index doesn't match up with the current index. What you're interested in is just setting the index to something else:

我认为你误解了什么reindex。它使用传递的索引沿传递的轴选择值,然后填充NaN传递的索引与当前索引不匹配的任何地方。您感兴趣的只是将索引设置为其他内容:

In [12]: df = DataFrame(randn(10, 2), columns=['a', 'delt'])

In [13]: df
Out[13]:
       a   delt
0  0.222 -0.964
1  0.038 -0.367
2  0.293  1.349
3  0.604 -0.855
4 -0.455 -0.594
5  0.795  0.013
6 -0.080 -0.235
7  0.671  1.405
8  0.436  0.415
9  0.840  1.174

In [14]: df.reindex(index=arange(1, len(df) + 1))
Out[14]:
        a   delt
1   0.038 -0.367
2   0.293  1.349
3   0.604 -0.855
4  -0.455 -0.594
5   0.795  0.013
6  -0.080 -0.235
7   0.671  1.405
8   0.436  0.415
9   0.840  1.174
10    NaN    NaN

In [16]: df.index = arange(1, len(df) + 1)

In [17]: df
Out[17]:
        a   delt
1   0.222 -0.964
2   0.038 -0.367
3   0.293  1.349
4   0.604 -0.855
5  -0.455 -0.594
6   0.795  0.013
7  -0.080 -0.235
8   0.671  1.405
9   0.436  0.415
10  0.840  1.174

Remember, if you want len(df)to be in the index you have to add 1 to the endpoint since Python doesn't include endpoints when constructing ranges.

请记住,如果您想len(df)在索引中,您必须将 1 添加到端点,因为 Python在构造 Ranges 时不包括端点