import pandas as pd data = pd.DataFrame(np.arange(1,10).reshape(3,3),index=["a","b","c"],columns=["A","B","C"]) # ============================================================================= # Out[13]: # A B C # a 1 2 3 # b 4 5 6 # c 7 8 9 # ============================================================================= data.set_index("A") # ============================================================================= # Out[14]: # B C # A # 1 2 3 # 4 5 6 # 7 8 9 # ============================================================================= #将A和C的列变成行索引,层次化索引 data.set_index(["A","C"]) # ============================================================================= # Out[17]: # B # A C # 1 3 2 # 4 6 5 # 7 9 8 # ============================================================================= data = pd.DataFrame(np.arange(1,10).reshape(3,3),index=["a","b","c"],columns=["A","B","C"]) data.set_index(["C"]) # ============================================================================= # Out[19]: # A B # C # 3 1 2 # 6 4 5 # 9 7 8 # ============================================================================= data.reset_index() # ============================================================================= # Out[24]: # index A B C # 0 a 1 2 3 # 1 b 4 5 6 # 2 c 7 8 9 # =============================================================================