});

DataFrame Transformation

transform the dataframe into various shapes

.stack() and .unstack()

.stack()命令将dataframe中的各列中转换为行,根据existing index进行分类,原本的columns作为新一层的index。
.unstack()命令则将行转换为series后,以选定的index作为columns转换为列
此处以行列调换为例
这两个命令都可以通过直接传入Index name 或 Index level 指定操作对象。

# make some data
info_3 = pd.DataFrame(data=np.arange(0,6,1).reshape(3,2),columns=['a','b'],index=range(3))
info_3


a
b
001
123
245
info_1 = info_3

# stack the column a,b into row 0,1,2 
info_1 = info_1.stack()
info_1
 0  a    0
    b    1
 1  a    2
    b    3
 2  a    4
    b    5
 dtype: int64 
# rename the level 1 col, i.e., 'a','b' col
# just demonstration of stack or rename the index
# not necessary in this procedure
info_1.index = info_1.index.rename('new_col',level=1)

# unstack the column level 0
# i.e., col '0', '1', '2'
info_1 = info_1.unstack(level=0)
info_1
012
new_col
a024
b135

.melt()

pass

Advanced

https://www.jianshu.com/p/0f8465f094e8