0

I know that[:,-1] means in pandas to use every row element and the last column element. But currently I see in the pandas documentation this one [::-1] and I wonder the meaning behind it?

Thanks

ddd
  • 167
  • 1
  • 7

1 Answers1

1

The optional 3rd number in the __getitem__ is the step. For pandas dataframes and series, as with python lists, when the third value is -1, you get the items in reverse order. For example

import pandas as pd

df = pd.DataFrame([[1], [2]])
# prints a row with 1, then a row with 2
print(df)
# prints a row with 2, then a row with 1
print(df[::-1])