gpt4 book ai didi

python - pandas:如何重新索引多索引级别?

转载 作者:行者123 更新时间:2023-11-30 22:16:42 27 4
gpt4 key购买 nike

按其中一个级别排序后,如何对 MultiIndex 级别重新编号?这是排序后的 DataFrame:

+--------+---+------+
| | | text |
+--------+---+------+
| letter | | |
+--------+---+------+
| a | 0 | blah |
+--------+---+------+
| | 3 | blah |
+--------+---+------+
| | 6 | blah |
+--------+---+------+
| b | 1 | blah |
+--------+---+------+
| | 4 | blah |
+--------+---+------+
| | 7 | blah |
+--------+---+------+
| c | 2 | blah |
+--------+---+------+
| | 5 | blah |
+--------+---+------+
| | 8 | blah |
+--------+---+------+

这就是我想要的(但可能将原始索引留在自己的列中):

+--------+---+------+
| | | text |
+--------+---+------+
| letter | | |
+--------+---+------+
| a | 0 | blah |
+--------+---+------+
| | 1 | blah |
+--------+---+------+
| | 2 | blah |
+--------+---+------+
| b | 0 | blah |
+--------+---+------+
| | 1 | blah |
+--------+---+------+
| | 2 | blah |
+--------+---+------+
| c | 0 | blah |
+--------+---+------+
| | 1 | blah |
+--------+---+------+
| | 2 | blah |
+--------+---+------+

我尝试过寻找答案,尝试编写不同的代码,但我被难住了。

重现上面第一个表的代码:

import pandas as pd
df = pd.DataFrame({'letter': ['a', 'b', 'c'] * 3, 'text': ['blah'] * 9})
df.set_index(keys='letter', append=True, inplace=True)
df = df.reorder_levels(order=[1, 0])
df.sort_index(level=0, inplace=True)
print(df)

最佳答案

您可以检查cumcount

df=df.assign(yourindex=df.groupby('letter').cumcount()).set_index(['letter','yourindex']).sort_index(level=[0,1])
df
Out[861]:
text
letter yourindex
a 0 blah
1 blah
2 blah
b 0 blah
1 blah
2 blah
c 0 blah
1 blah
2 blah

关于python - pandas:如何重新索引多索引级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49887258/

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