常见的数据的层次化结构有两种,一种是表格,一种是“花括号”,stack函数会将数据从”表格“结构变成”花括号“结构,反之,unstack函数将数据从”花括号“结构变成”表格“结构
# 导入包
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
网友评论