Create column named “Id” based on row index

后端 未结 2 1440
庸人自扰
庸人自扰 2021-01-13 16:50

I would like to create a new column for my dataframe named \"Id\" where the value is the row index +1. I would like to be like the example below:

   ID  Col         


        
2条回答
  •  感情败类
    2021-01-13 17:34

    You can add one to the index and assign it to the id column:

    df = pd.DataFrame({"Col1": list("abc")})
    
    df["id"] = df.index + 1
    
    df
    #Col1   id
    #0  a    1
    #1  b    2
    #2  c    3
    

提交回复
热议问题