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 指定操作对象。
1 2 3 |
# 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 | |
---|---|---|
0 | 0 | 1 |
1 | 2 | 3 |
2 | 4 | 5 |
1 2 3 4 5 |
info_1 = info_3 # stack the column a,b into row 0,1,2 info_1 = info_1.stack() info_1 |
1 2 3 4 5 6 7 |
0 a 0 b 1 1 a 2 b 3 2 a 4 b 5 dtype: int64 |
1 2 3 4 5 6 7 8 9 |
# 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 |
0 | 1 | 2 | |
---|---|---|---|
new_col | |||
a | 0 | 2 | 4 |
b | 1 | 3 | 5 |
.melt()
pass