Pandas:多级索引的操作--调整顺序、排序、汇总、指定列为索引

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
df = DataFrame(np.arange(12).reshape((4,3)),
                 index = [['a','a','b','b'],[1,2,1,2]],
                 columns = [['Ohio','Ohio','Colorado'],['Green','Red','Green']])
df.index.names = ['key1','key2']
df.columns.names = ['state','color']
print(df)
state      Ohio     Colorado
color     Green Red    Green
key1 key2                   
a    1        0   1        2
     2        3   4        5
b    1        6   7        8
     2        9  10       11

一、调整多级索引间的顺序

print(df.swaplevel('key1','key2'))
state      Ohio     Colorado
color     Green Red    Green
key2 key1                   
1    a        0   1        2
2    a        3   4        5
1    b        6   7        8
2    b        9  10       11

二、对某级别索引排序

print(df.sort_index(level=1))
state      Ohio     Colorado
color     Green Red    Green
key1 key2                   
a    1        0   1        2
b    1        6   7        8
a    2        3   4        5
b    2        9  10       11

三、根据某级别索引进行汇总

print(df.sum(level='color',axis=1))
color      Green  Red
key1 key2            
a    1         2    1
     2         8    4
b    1        14    7
     2        20   10

四、指定DataFrame的列为索引

data = {
  '种类':['苹果','梨','草莓'],
    '数量':[3,2,5],
       '价格':[10,9,8]}
df = DataFrame(data)

指定

df = df.set_index('种类')
print(df)
    价格  数量
种类        
苹果  10   3
梨    9   2
草莓   8   5

还原

print(df.reset_index())
   种类  价格  数量
0  苹果  10   3
1   梨   9   2
2  草莓   8   5
    原文作者:BQW_
    原文地址: https://blog.csdn.net/bqw18744018044/article/details/79944273
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞