0

hello is there a way I could reverse the order of the rows. I have used the function data1 = data.iloc[::-1] which reverses all the values within the data table including the row numbers, however I only want it to reverse the values and leave the rows as is. So I would like row 0 : Open:242.5 , row 1 : Open:243.95 and so on. At the image below you can also see how the rows start at row#; 1862 and should be row#; 1862

enter image description here

  • Does this answer your question? [Right way to reverse pandas.DataFrame?](https://stackoverflow.com/questions/20444087/right-way-to-reverse-pandas-dataframe) One of the answers there mentions resetting the index. – noah Dec 14 '20 at 22:20

3 Answers3

1

You can just use a reset_index to reset your index after you've reversed it.

data=data.iloc[::-1].reset_index(drop=True)
Red
  • 110
  • 1
  • 9
0

Just reset_index() after.

df.reset_index(drop=True, inplace=True)
noah
  • 2,616
  • 13
  • 27
0

Just reset the index in case of ordered indexes:

data1 = data.iloc[::-1].reset_index(drop=True)

Or else save the previous index and apply it again, this will work even if the index os not ordered:

indexes = data.index
data1 = data.iloc[::-1]
data1.index = indexes
adir abargil
  • 5,495
  • 3
  • 19
  • 29