0

So I've been thinking of different solutions for creating a pandas index where the number corresponds to the row number. I think I'm either overthinking it or missing some god simple solution. I've spent way to many hours thinking of this.

As of now I'm doing it with a for loop running through df.iterrows(), adding each iteration into a list, then adding the list to pandas, then making that the index.

What I have:

apple 5 3
banana 3  2
pear 4  5

what I want:

1 Apple 5 3 
2 Banana 3 2
3 Pear 4 5
linkedby
  • 148
  • 1
  • 8
  • 4
    `df=df.reset_index()`? or if you want that your index starts with 1 then use `df.index=df.reset_index().index+1` – Anurag Dabas May 19 '21 at 10:07
  • would be better to ditch the `iterrows` approach all together, can you show your starting data and the code your employing? – Umar.H May 19 '21 at 10:09
  • df.index=df.reset_index().index+1 was exactly what I was looking for. Have totally missed it's possible to use "+1". – linkedby May 19 '21 at 10:12
  • Why not `RangeIndex: `df.set_index(pd.RangeIndex(1, len(df)+1))` – Corralien May 19 '21 at 11:55

0 Answers0