ddd
ddd

Reputation: 167

What is the meaning of this kind of indexing [::-1] in pandas?

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

Upvotes: 0

Views: 176

Answers (1)

Mahesh Vashishtha
Mahesh Vashishtha

Reputation: 176

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])

Upvotes: 1

Related Questions