Python 在熊猫中设置多列索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41208610/
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
Set multi column index in pandas
提问by alexmoon
I make dataframe like this.
我制作这样的数据框。
df = pd.DataFrame({
'class' : ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'],
'number' : [1,2,3,4,5,1,2,3,4,5],
'math' : [90, 20, 50, 30, 57, 67, 89, 79, 45, 23],
'english' : [40, 21, 68, 89, 90, 87, 89, 54, 21, 23]
})
and i want to convert index to this by using some pandas methods.(ex. set_index, stack,,,)
我想通过使用一些 Pandas 方法将索引转换为这个。(例如 set_index、stack、、)
df1 = pd.DataFrame(np.random.randint(1, 100, (5, 4)),
columns = [['A', 'A', 'B', 'B'],['english', 'math', 'english', 'math']],
index = [1, 2, 3, 4, 5])
how can i do this?
我怎样才能做到这一点?
回答by jezrael
I think you need set_index
with unstack
for reshaping, then swap levels in MultiIndex
in columns by swaplevel
and last sort columns by sort_index
:
我认为你需要set_index
与unstack
重塑,然后交换在水平MultiIndex
由列swaplevel
由去年的排序列sort_index
:
df1 = df.set_index(['number','class']).unstack().swaplevel(0,1,1).sort_index(1)
print (df1)
class A B
english math english math
number
1 40 90 87 67
2 21 20 89 89
3 68 50 54 79
4 89 30 21 45
5 90 57 23 23
Another solution with stack
and unstack
:
print (df.set_index(['number','class']).stack().unstack([1,2]))
class A B
english math english math
number
1 40 90 87 67
2 21 20 89 89
3 68 50 54 79
4 89 30 21 45
5 90 57 23 23
回答by Roman Pekar
I like @jezrael answer a lot, but just for completeness - you can also use pandas.DataFrame.pivot_table
instead of set_index
+ unstack
:
我很喜欢@jezrael 的回答,但只是为了完整性 - 您也可以使用+pandas.DataFrame.pivot_table
代替:set_index
unstack
>>> df.pivot_table(index='number', columns='class').swaplevel(axis=1).sort_index(1)
class A B
english math english math
number
1 40 90 87 67
2 21 20 89 89
3 68 50 54 79
4 89 30 21 45
5 90 57 23 23