python - Pandas - Dataframe.set_index - how to keep the old index column

后端 未结 5 1104
借酒劲吻你
借酒劲吻你 2021-02-05 06:36

I have this Dataframe:

import pandas as pd
df = pd.DataFrame({\'Hugo\' : {\'age\' : 21, \'weight\' : 75},
                   \'Bertram\': {\'age\' :         


        
5条回答
  •  萌比男神i
    2021-02-05 07:14

    Adding the append=True and with reset_index

    df.set_index('age', append=True).reset_index(level=0)
    Out[80]: 
            name  weight
    age                 
    45   Bertram      65
    75    Donald      85
    21      Hugo      75
    

提交回复
热议问题