转换数据
df.stack() 列索引→行索引 df.unstack() 行索引→列索引
参数 | 类型 | 说明 |
level | int string list | -1 默认值 默认将最内层的列索引转换为最内层的行索引 |
dropna | bool | True 默认值 数据转换后,如果一行数据均为缺省值Nan,则删除掉 |
>>> df1
weight height
cat 0 1
dog 2 3
#Stacking a dataframe with a single level column axis returns a Series
>>> df1.stack()
cat weight 0
height 1
dog weight 2
height 3
-----------------------------------------------------------------------------------
>>> df2
weight height
kg m
cat 1.0 2.0
dog 3.0 4.0
#默认将最内层的列索引转换为最内层行索引
>>> df2.stack()
height weight
cat kg NaN 1.0
m 2.0 NaN
dog kg NaN 3.0
m 4.0 NaN
>>> df2.stack(0)
kg m
cat height NaN 2.0
weight 1.0 NaN
dog height NaN 4.0
weight 3.0 NaN
>>> df2.stack(-1) / df2.stack(1)
height weight
cat kg NaN 1.0
m 2.0 NaN
dog kg NaN 3.0
m 4.0 NaN
>>> df2.stack([0, -1]) / df2.stack([0, 1])
cat height m 2.0
weight kg 1.0
dog height m 4.0
weight kg 3.0
>>> df2.stack([-1, 0]) / df2.stack([1, 0])
cat kg weight 1.0
m height 2.0
dog kg weight 3.0
m height 4.0
-----------------------------------------------------------------------------------
>>> df3
weight height
kg m
cat NaN 1.0
dog 2.0 3.0
#stack之后产生缺省数据,用NaN填充
>>> df3.stack(dropna=False)
height weight
cat kg NaN NaN
m 1.0 NaN
dog kg NaN 2.0
m 3.0 NaN
#stack之后,如果一行全是NaN则删除数据行(默认操作)
>>> df3.stack() / df3.stack(dropna=True)
height weight
cat m 1.0 NaN
dog kg NaN 2.0
m 3.0 NaN
-----------------------------------------------------------------------------------
#Set df2 columns & index 's Name
>>> df2.columns.names=['Lable','Data']
>>> df2
Lable weight height
Data kg m
cat 1.0 2.0
dog 3.0 4.0
>>> df2.stack('Lable')
Data kg m
Lable
cat height NaN 2.0
weight 1.0 NaN
dog height NaN 4.0
weight 3.0 NaN
>>> df2.stack('Data')
Lable height weight
Data
cat kg NaN 1.0
m 2.0 NaN
dog kg NaN 3.0
m 4.0 NaN
>>> df2.stack(['Lable','Data'])
Lable Data
cat height m 2.0
weight kg 1.0
dog height m 4.0
weight kg 3.0
>>> df2.stack(['Data','Lable'])
Data Lable
cat kg weight 1.0
m height 2.0
dog kg weight 3.0
m height 4.0