Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Reverse the Rows of a Pandas Data Frame?
We will see here how to reverse the rows of a Pandas DataFrame. Pandas is an open-source Python library providing high-performance data manipulation and analysis tool using its powerful data structures. A DataFrame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns.
Using Indexing with [::-1]
The simplest method to reverse DataFrame rows is using slice notation [::-1]
import pandas as pd
# Create a DataFrame
data = {'Rank': [1, 2, 3, 4, 5], 'Points': [100, 87, 80, 70, 50]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# Reverse the DataFrame using indexing
print("\nReversed DataFrame:")
print(df[::-1])
Original DataFrame: Rank Points 0 1 100 1 2 87 2 3 80 3 4 70 4 5 50 Reversed DataFrame: Rank Points 4 5 50 3 4 70 2 3 80 1 2 87 0 1 100
Using reindex() Method
The reindex() method allows you to reorder rows by specifying a new index order
import pandas as pd
data = {'Rank': [1, 2, 3, 4, 5], 'Points': [100, 87, 80, 70, 50]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# Reverse using reindex()
reversed_df = df.reindex(index=df.index[::-1])
print("\nReversed DataFrame:")
print(reversed_df)
Original DataFrame: Rank Points 0 1 100 1 2 87 2 3 80 3 4 70 4 5 50 Reversed DataFrame: Rank Points 4 5 50 3 4 70 2 3 80 1 2 87 0 1 100
Using iloc Property
The iloc property enables integer-location based indexing for selection by position
import pandas as pd
data = {'Rank': [1, 2, 3, 4, 5], 'Points': [100, 87, 80, 70, 50]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# Reverse using iloc
reversed_df = df.iloc[::-1]
print("\nReversed DataFrame:")
print(reversed_df)
Original DataFrame: Rank Points 0 1 100 1 2 87 2 3 80 3 4 70 4 5 50 Reversed DataFrame: Rank Points 4 5 50 3 4 70 2 3 80 1 2 87 0 1 100
Using sort_index() Method
The sort_index() method can reverse row order by setting ascending=False
import pandas as pd
data = {'Rank': [1, 2, 3, 4, 5], 'Points': [100, 87, 80, 70, 50]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# Reverse using sort_index()
reversed_df = df.sort_index(ascending=False)
print("\nReversed DataFrame:")
print(reversed_df)
Original DataFrame: Rank Points 0 1 100 1 2 87 2 3 80 3 4 70 4 5 50 Reversed DataFrame: Rank Points 4 5 50 3 4 70 2 3 80 1 2 87 0 1 100
Using reset_index() to Reset Index
If you want to reverse rows and reset the index to start from 0, combine reversal with reset_index()
import pandas as pd
data = {'Rank': [1, 2, 3, 4, 5], 'Points': [100, 87, 80, 70, 50]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# Reverse and reset index
reversed_df = df[::-1].reset_index(drop=True)
print("\nReversed DataFrame with Reset Index:")
print(reversed_df)
Original DataFrame: Rank Points 0 1 100 1 2 87 2 3 80 3 4 70 4 5 50 Reversed DataFrame with Reset Index: Rank Points 0 5 50 1 4 70 2 3 80 3 2 87 4 1 100
Comparison
| Method | Syntax | Preserves Original Index | Best For |
|---|---|---|---|
| Slicing | df[::-1] |
Yes | Simple and readable |
| reindex() | df.reindex(df.index[::-1]) |
Yes | Explicit control over indexing |
| iloc | df.iloc[::-1] |
Yes | Position-based selection |
| sort_index() | df.sort_index(ascending=False) |
Yes | When working with sorted data |
| With reset_index() | df[::-1].reset_index(drop=True) |
No | When new sequential index needed |
Conclusion
Use df[::-1] for the simplest row reversal. Use reset_index(drop=True) if you need a fresh sequential index. All methods achieve the same result with slight differences in syntax and index handling.
