gpt4 book ai didi

python - 就地重新排序 DataFrame 行

转载 作者:太空宇宙 更新时间:2023-11-03 15:03:15 24 4
gpt4 key购买 nike

是否可以就地重新排序pandas.DataFrame行(基于索引)?我正在寻找这样的东西:

df.reorder(idx, axis=0, inplace=True)

其中idx不等于df.index但类型相同,它包含相同的元素,但顺序不同。输出在新的 idx 之前重新排序了 df

我在文档中没有找到任何内容,并且无法使用 reindex_axis。这让我希望这是可能的,因为:

A new object is produced unless the new index is equivalent to the current one and copy=False

我可能误解了等效索引在这种情况下的含义。

最佳答案

尝试使用reindex函数(请注意,这不是就位):

>>import pandas as pd

>>df = pd.DataFrame({'col1':[1,2,3],'col2':['test','hi','hello']})
>>df
col1 col2
0 1 test
1 2 hi
2 3 hello


>>df = df.reindex([2,0,1])
>>df
col1 col2
2 3 hello
0 1 test
1 2 hi

关于python - 就地重新排序 DataFrame 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44887590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com