How to convert index of a pandas dataframe into a column?

前端 未结 7 707
后悔当初
后悔当初 2020-11-22 08:54

This seems rather obvious, but I can\'t seem to figure out how to convert an index of data frame to a column?

For example:

df=
        gi       ptt_l         


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 09:33

    df1 = pd.DataFrame({"gi":[232,66,34,43],"ptt":[342,56,662,123]})
    p = df1.index.values
    df1.insert( 0, column="new",value = p)
    df1
    
        new     gi     ptt
    0    0      232    342
    1    1      66     56 
    2    2      34     662
    3    3      43     123
    

提交回复
热议问题