How can I swap two rows in a Pandas dataframe? Can you provide an example?

How can I swap two rows in a Pandas dataframe? Can you provide an example?

To swap two rows in a Pandas dataframe, you can use the .iloc function and specify the index of the rows you want to swap. For example, to swap the first and second rows, you can use the code df.iloc[[1,0]] = df.iloc[[0,1]]. This will interchange the values of the first and second rows in the dataframe.

Swap Two Rows in Pandas (With Example)


You can use the following custom function to swap the position of two rows in a pandas DataFrame:

defswap_rows(df, row1, row2):
    df.iloc[row1], df.iloc[row2] =  df.iloc[row2].copy(), df.iloc[row1].copy()
    return df

This function will swap the positions of rows in index positions row1 and row2 in the DataFrame.

The following example shows how to use this function in practice.

Example: Swap Two Rows in Pandas

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team' : ['Mavs', 'Nets', 'Kings', 'Cavs', 'Heat', 'Magic'],
                   'points' : [12, 15, 22, 29, 24, 22],
                   'assists': [4, 5, 10, 8, 7, 10]})

#view DataFrame
print(df)

    team  points  assists
0   Mavs      12        4
1   Nets      15        5
2  Kings      22       10
3   Cavs      29        8
4   Heat      24        7
5  Magic      22       10

We can define a swap_rows() function to swap the rows in index positions 0 and 4 in the DataFrame:

#define function to swap rows
defswap_rows(df, row1, row2):
    df.iloc[row1], df.iloc[row2] =  df.iloc[row2].copy(), df.iloc[row1].copy()
    return df

#swap rows in index positions 0 and 4
df = swap_rows(df, 0, 4)

#view updated DataFrame
print(df)

    team  points  assists
0   Heat      24        7
1   Nets      15        5
2  Kings      22       10
3   Cavs      29        8
4   Mavs      12        4
5  Magic      22       10

Notice that the rows in index positions 0 and 4 have been swapped while every other row has remained in the same position.

Note: Within the swap_rows() function, we used the function to select rows in the DataFrame based on their index position.

The following tutorials explain how to perform other common tasks in pandas:

Cite this article

stats writer (2024). How can I swap two rows in a Pandas dataframe? Can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-swap-two-rows-in-a-pandas-dataframe-can-you-provide-an-example/

stats writer. "How can I swap two rows in a Pandas dataframe? Can you provide an example?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-swap-two-rows-in-a-pandas-dataframe-can-you-provide-an-example/.

stats writer. "How can I swap two rows in a Pandas dataframe? Can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-swap-two-rows-in-a-pandas-dataframe-can-you-provide-an-example/.

stats writer (2024) 'How can I swap two rows in a Pandas dataframe? Can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-swap-two-rows-in-a-pandas-dataframe-can-you-provide-an-example/.

[1] stats writer, "How can I swap two rows in a Pandas dataframe? Can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I swap two rows in a Pandas dataframe? Can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top