MEhsan MEhsan - 1 year ago 144
Python Question

How to Add Incremental Numbers to a New Column Using Pandas

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 column
which has the number
that increments by one in each row.

The output should be simply like:

New_ID ID Fruit
880 F1 Apple
881 F2 Orange
882 F3 Banana

I tried the following:

df['New_ID'] = ["880"] # but I want to do this without assigning it the list of numbers literally

Any idea how to solve this?


Answer Source


df = df.reset_index()
df.columns[0] = 'New_ID'
df['New_ID'] = df.index + 880
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download