Pandas:多級索引的操作--調整順序、排序、彙總、指定列為索引
阿新 • • 發佈:2019-02-01
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