10分钟
索引旋转
1. DataFrame.stack()
方法将数据的列索引旋转为行索引。注意:它跟转置不同,转置会同时旋转数据。
DataFrame.stack(level=-1, dropna=True)
DataFrame.unstack(level=-1, fill_value=None)
2. DataFrame.pivot()
方法重排数据。它是一个快捷方式,它使用set_index
将列数据变成行索引,然后使用unstack
将行索引转为列索引。
DataFrame.pivot(index=None, columns=None, values=None)
实际上对 index,clumns 指定的列数据均 set_index,然后仅对 columns 对应的列数据 unstack
values
:一个字符串,指定了哪个列数据作为结果的数据。如果未提供,则剩余的所有列都将作为结果的数据。
学员评价