Table of Contents
Swapping two columns in a Pandas dataframe refers to the action of rearranging the order of two columns within the dataframe. This can be done by using the “iloc” function and specifying the columns to be swapped. It is a useful tool for reorganizing data and can be achieved by following a few simple steps. First, identify the names or indices of the columns to be swapped. Then, use the “iloc” function to select the columns and assign them to temporary variables. Finally, use the “iloc” function again to reassign the columns in the desired order. This process allows for the efficient and effective manipulation of data within a Pandas dataframe.
Swap Two Columns in Pandas (With Example)
You can use the following custom function to swap the position of two columns in a pandas DataFrame:
defswap_columns(df, col1, col2): col_list = list(df.columns) x, y = col_list.index(col1), col_list.index(col2) col_list[y], col_list[x] = col_list[x], col_list[y] df = df[col_list] return df
This function will swap the positions of columns col1 and col2 in the DataFrame.
The following example shows how to use this function in practice.
Example: Swap Two Columns in Pandas
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], 'points': [18, 22, 19, 14, 14, 11, 20, 28], 'assists': [5, 7, 7, 9, 12, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #view DataFrame print(df) team points assists rebounds 0 A 18 5 11 1 B 22 7 8 2 C 19 7 10 3 D 14 9 6 4 E 14 12 6 5 F 11 9 5 6 G 20 9 9 7 H 28 4 12
We can define a swap_columns() function to swap the positions of the “points” and “rebounds” columns:
#define function to swap columns defswap_columns(df, col1, col2): col_list = list(df.columns) x, y = col_list.index(col1), col_list.index(col2) col_list[y], col_list[x] = col_list[x], col_list[y] df = df[col_list] return df #swap points and rebounds columns df = swap_columns(df, 'points', 'rebounds'): #view updated DataFrame print(df) team rebounds assists points 0 A 11 5 18 1 B 8 7 22 2 C 10 7 19 3 D 6 9 14 4 E 6 12 14 5 F 5 9 11 6 G 9 9 20 7 H 12 4 28
Notice that the “points” and “rebounds” columns have been swapped while every other column has remained in the same position.
Additional Resources
The following tutorials explain how to perform other common operations in pandas:
Cite this article
stats writer (2024). How can I swap two columns in a Pandas dataframe?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-swap-two-columns-in-a-pandas-dataframe/
stats writer. "How can I swap two columns in a Pandas dataframe?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-swap-two-columns-in-a-pandas-dataframe/.
stats writer. "How can I swap two columns in a Pandas dataframe?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-swap-two-columns-in-a-pandas-dataframe/.
stats writer (2024) 'How can I swap two columns in a Pandas dataframe?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-swap-two-columns-in-a-pandas-dataframe/.
[1] stats writer, "How can I swap two columns in a Pandas dataframe?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I swap two columns in a Pandas dataframe?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
