set_index:设置索引
reset_index :清除索引列,默认全部。行索引到列索引去了
stack:堆叠行索引
unstack:不堆叠行索引
- 准备数据
frame = pd.DataFrame(np.arange(12).reshape((4, 3)), index=[['a', 'a', 'b', 'b'], [1, 2, 1, 2]], columns=[['Ohio', 'Ohio', 'Colorado'], ['Green', 'Red', 'Green']] ) frame.index.names=['key1','key2'] frame.columns.names=['state','color'] state Ohio Colorado color Green Red Green key1 key2 a 1 0 1 2 2 3 4 5 b 1 6 7 8 2 9 10 11
-
reset_index 清除索引 默认清除全部索引。
第二种是清除key1的索引
-
set_index() 设置索引 制定列来设置。需要传入值,没有默认值
-
unstack() 不堆叠 默认行的最里层 堆叠到列的最里层。设置值,则设置制定的行堆叠到最里列。
-
stack() 设置堆叠,不设置值默认列的最里层,设置值,则堆叠制定的索引
-