pandas
DataFrame
reversing DataFrame
Python
data manipulation

Right way to reverse a pandas DataFrame?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Reversing a pandas DataFrame can mean reversing the row order, the column order, or both. The most common case is row reversal, and the cleanest technique is usually slicing with iloc[::-1]. The right choice also depends on whether you want to preserve the original index labels or reset them after reversal.

Reverse Rows with iloc[::-1]

To reverse row order, use integer-location slicing with a negative step.

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "name": ["Ava", "Liam", "Noah"],
5    "score": [91, 88, 95],
6})
7
8reversed_rows = df.iloc[::-1]
9print(reversed_rows)

This is the standard idiomatic solution and works for most DataFrames.

Reset the Index If Needed

After reversing, the original index labels remain attached to the rows. That is often correct, but sometimes you want a clean sequential index.

python
reversed_reset = df.iloc[::-1].reset_index(drop=True)
print(reversed_reset)

Use this form when the reversed DataFrame will be exported, displayed, or joined later as a fresh table rather than as the same indexed dataset in different order.

Reverse Columns Instead of Rows

If your goal is to reverse columns, slice on the second axis.

python
reversed_columns = df.iloc[:, ::-1]
print(reversed_columns)

You can reverse both rows and columns together if you really want a full matrix-style reversal:

python
both = df.iloc[::-1, ::-1]
print(both)

When to Use sort_index Instead

If the real intent is “highest index to lowest index,” and not simply “current row order reversed,” then sort_index(ascending=False) may better match the requirement.

python
1df2 = pd.DataFrame(
2    {"value": [10, 20, 30]},
3    index=[101, 103, 102]
4)
5
6print(df2.sort_index(ascending=False))

This is different from iloc[::-1], which reverses the current order without interpreting the index.

Reverse After Filtering or Grouped Work

In real pipelines, reversal often happens after filtering, sorting, or selecting recent records. Keep the operation order intentional. Reversing first and then sorting again usually wastes work and can hide what the final ordering contract actually is.

For example, if you want newest rows first, sort by timestamp instead of reversing a frame whose order came from an earlier accidental step. Use reversal only when the current order itself is meaningful and simply needs to be flipped.

Copying and Chained Operations

Slicing may return a view-like object depending on operation context, so if you plan to mutate the reversed result independently, make an explicit copy.

python
rev = df.iloc[::-1].copy()
rev["passed"] = rev["score"] >= 90
print(rev)

Explicit .copy() avoids later confusion about whether modifications affect the original frame or trigger warning-prone behavior.

Performance Considerations

For normal DataFrame work, iloc[::-1] is efficient and readable. You do not need more complicated approaches unless profiling proves a bottleneck. The bigger concern is usually what you do after reversal, especially if you repeatedly reset indexes or materialize extra copies in large pipelines.

MultiIndex Considerations

With MultiIndex DataFrames, a simple reversal still reverses the current row order, but it does not semantically reorder specific levels. If your requirement is to reverse one index level while preserving the rest, use index-aware operations rather than generic slicing.

That distinction matters when working with grouped time-series or hierarchical report tables.

Common Pitfalls

  • Using iloc[::-1] when you really meant descending sort by index or a column.
  • Forgetting that reversed rows keep their original index labels.
  • Mutating a reversed frame without making a copy first when independence matters.
  • Reversing both axes accidentally when only rows were intended.
  • Assuming row reversal changes the semantic ordering of a MultiIndex level.

Summary

  • Use df.iloc[::-1] to reverse rows in the current order.
  • Add .reset_index(drop=True) if you want a fresh sequential index.
  • Use df.iloc[:, ::-1] to reverse columns.
  • Prefer sort_index or column sorting when you need semantic ordering, not simple reversal.
  • Use .copy() if the reversed result will be modified independently.

Course illustration
Course illustration

All Rights Reserved.