常見的數(shù)據(jù)的層次化結構有兩種,一種是表格,一種是“花括號”,stack函數(shù)會將數(shù)據(jù)從”表格“結構變成”花括號“結構,反之,unstack函數(shù)將數(shù)據(jù)從”花括號“結構變成”表格“結構
# 導入包
import pandas as pd
import numpy as np
# 在同一個cell里同時輸出多條語句
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
df=pd.DataFrame(np.arange(6).reshape((2,3))
,index=pd.Index(['street1','street2'])
,columns=pd.Index(['one','two','three']))
df2=df.stack()
df2.unstack()
image.png
