数据重构

2017-09-07  本文已影响40人  b485c88ab697

数据重构

import numpy as np
import pandas as pd

stack

df_obj = pd.DataFrame(np.random.randint(0,10, (5,2)), columns=['data1', 'data2'])
df_obj
Paste_Image.png
stacked = df_obj.stack()
print(stacked)
0  data1    4
   data2    4
1  data1    9
   data2    9
2  data1    4
   data2    6
3  data1    5
   data2    0
4  data1    9
   data2    7
dtype: int32
print(type(stacked))
print(type(stacked.index))
<class 'pandas.core.series.Series'>
<class 'pandas.core.indexes.multi.MultiIndex'>

默认操作内层索引

stacked.unstack()
Paste_Image.png

通过level指定操作索引的级别

stacked.unstack(level=0)
Paste_Image.png
上一篇下一篇

猜你喜欢

热点阅读