[英]How to convert a DataFrame with repeated indices into a MultiIndex DataFrame?
I'm trying to convert DataFrame that looks like this: 我正在尝试转换看起来像这样的DataFrame:
i j n
1 1 2 #
2 1 3 #
3 1 5 #
4 2 1 #
5 2 2 #
6 2 4 #
7 3 5 #
8 3 6 #
to a MultiIndex DataFrame like this: 到这样的MultiIndex DataFrame:
n
1 2 #
3 #
5 #
2 1 #
2 #
4 #
3 5 #
6 #
I don't want to go row by row and inserting into a new DataFrame and I'm sure that there's a quicker way anyway. 我不想一行一行地插入到新的DataFrame中,我确信无论如何都有更快的方法。
Welp,我最初误解了文档,解决方案非常简单: df.set_index(['i', 'j'])
产生预期的效果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.