How to Add Incremental Numbers to a New Column Using Pandas

前端 未结 6 1066
感情败类
感情败类 2020-12-24 04:55

I have this simplified dataframe:

ID   Fruit
F1   Apple
F2   Orange
F3   Banana 

I want to add in the begining of the dataframe a new colum

6条回答
  •  一向
    一向 (楼主)
    2020-12-24 05:17

    For a pandas DataFrame whose index starts at 0 and increments by 1 (i.e., the default values) you can just do:

    df.insert(0, 'New_ID', df.index + 880)
    

    if you want New_ID to be the first column. Otherwise this if you don't mind it being at the end:

    df['New_ID'] = df.index + 880
    

提交回复
热议问题