Pandas: How to swap 2 rows in a DataFrame (3 approaches)

Updated: February 21, 2024 By: Guest Contributor Post a comment

Overview

Swapping rows in a DataFrame is a common task that data scientists and analysts encounter while preprocessing data. Pandas, being the go-to library for data manipulation in Python, offers a flexible approach to manipulate DataFrame structures in various ways, including swapping rows. This tutorial walks you through multiple methods to swap two rows in a Pandas DataFrame, ranging from basic to more advanced techniques.

Preliminaries

Before diving into the different methods of swapping rows, it’s important to have your environment setup. This includes installing the Pandas library if you haven’t already. You can install Pandas via pip:

pip install pandas

Once installed, you need to import Pandas in your Python script or Jupyter notebook:

import pandas as pd

For the sake of examples, let’s create a simple DataFrame:

df = pd.DataFrame({
  'A': [1, 2, 3, 4],
  'B': ['a', 'b', 'c', 'd']
})
print(df)

Output:

   A  B
0  1  a
1  2  b
2  3  c
3  4  d

Method 1: Simple Row Swap

One basic but straightforward method to swap two rows involves directly manipulating the DataFrame’s index. Suppose we want to swap rows 1 and 3. Here’s how you can do it:

df.iloc[[0, 2]] = df.iloc[[2, 0]].values
print(df)

Output:

   A  B
0  3  c
1  2  b
2  1  a
3  4  d

Method 2: Using the .loc[] Attribute

Another approach is to use the .loc[] attribute, which accesses a group of rows and columns by labels. Here, the technique involves copying the rows to be swapped into a temporary DataFrame and then reassigning them:

temp = df.loc[[1, 3]]
df.loc[[1, 3]] = temp.loc[[3, 1]].values
print(df)

Output:

   A  B
0  1  a
1  4  d
2  3  c
3  2  b

Method 3: Using a Custom Function

If you find yourself needing to swap rows more frequently, or if you’re dealing with more complex row manipulations, writing a custom function could be more efficient. Here’s an example of a custom function to swap any two rows in a DataFrame:

def swap_rows(df, idx1, idx2):
    a, b = df.iloc[idx1].copy(), df.iloc[idx2].copy()
    df.iloc[idx1], df.iloc[idx2] = b, a
    return df

# Example usage:
df = swap_rows(df, 1, 2)
print(df)

Output:

   A  B
0  1  a
1  3  c
2  2  b
3  4  d

For those dealing with larger datasets or more complex swap operations (such as conditional swaps based on DataFrame values), Harnessing the power of Pandas’s indexing can be particularly beneficial.

Conclusion

Swapping rows in a Pandas DataFrame may seem daunting at first, but with the right techniques, it can be straightforward and effective. Whether you’re pre-processing data or reorganizing it for visualization purposes, the methods outlined in this tutorial offer a good start. Importantly, always ensure to work with copies of your original DataFrame or to safeguard its integrity when manipulating it in complex ways.