数据分析--pandas(数据重构)

pandas

主要针对multiindex对象,使用stack()和unstack()方法

  • stack
    • 将列索引转为行索引,完成层级索引
    • DataFrame -->  Series
  • unstack
    • 将层级索引展开
    • Series  -->  DataFrame
    • 默认操作内层索引
import numpy as np
import pandas as pd


df_obj = pd.DataFrame(np.random.randint(0,10, (5,2)), columns=['data1', 'data2'])
df_obj

stacked = df_obj.stack()
print(stacked)
answer:
0  data1    9
   data2    0
1  data1    1
   data2    5
2  data1    7
   data2    3
3  data1    2
   data2    1
4  data1    6
   data2    5

print(type(stacked))
print(type(stacked.index))
answer:



# 默认操作内层索引
stacked.unstack()
answer
        data1	data2
0	9	0
1	1	5
2	7	3
3	2	1
4	6	5

# 通过level指定操作索引的级别
stacked.unstack(level=0)
        0	1	2	3	4
data1	9	1	7	2	6
data2	0	5	3	1	5

 

你可能感兴趣的:(python,pandas,stack,unstack)