pandas中的stack和unstack

举个例子就能看出stack和unstack的不同:

import numpy as np
import pandas as pd
from pandas import Series, DataFrame
data = DataFrame(np.arange(6).reshape((2, 3)), 
                 index = pd.Index(['street1', 'street2']), 
                 columns = pd.Index(['one', 'two', 'three']))
print(data)
print('\n-----------------------------------------\n')
data2=data.stack()
data3=data2.unstack()
print(data2)
print('\n-----------------------------------------\n')
print(data3)

输出结果:

pandas中的stack和unstack_第1张图片

你可能感兴趣的:(学习笔记)