Change column orders using column names list - Pandas Dataframe
Sometimes it's necessary to change the order in which a Pandas Dataframe's columns are displayed. For example, you might want to display non-key columns near the top of your Dataframe or display certain columns there. Here is how you can do that.
import pandas as pd
data = [
['a_row', 'a1', 'a2', 'a3'],
['b_row', 'b1', 'b2', 'b3'],
['c_row', 'c1', 'c2', 'c3']
]
# create dataframe
df = pd.DataFrame(data, columns=['col_row', 'col_1', 'col_2', 'col_3'], index=['a', 'b', 'c'])
print(df)
# change column orders using column names list
df = df[['col_3', 'col_2', 'col_row', 'col_1']]
print(df)
Output
+----+-----------+---------+---------+---------+
| | col_row | col_1 | col_2 | col_3 |
|----+-----------+---------+---------+---------|
| a | a_row | a1 | a2 | a3 |
| b | b_row | b1 | b2 | b3 |
| c | c_row | c1 | c2 | c3 |
+----+-----------+---------+---------+---------+
+----+---------+---------+-----------+---------+
| | col_3 | col_2 | col_row | col_1 |
|----+---------+---------+-----------+---------|
| a | a3 | a2 | a_row | a1 |
| b | b3 | b2 | b_row | b1 |
| c | c3 | c2 | c_row | c1 |
+----+---------+---------+-----------+---------+
Here we have created a Pandas Dataframe using a list named data. It contains multiple lists that contain some items. we have defined our columns name while creating the DataFrame. Now we have a requirement to change the order of the columns of the Dataframe. In order to do that, we can easily use the below code syntax.
df = df[[column_name_1, column_name_2, ...]]
We are rearranging our Pandas DataFrame columns here. When you convert them to a dictionary or list they will be in the same order as in the DataFrame. So it can be very helpful when you want to sort the column names based on a Python List that contain the names of the columns.
Another Example of rearranging DataFrame columns
Here is another example of changing the orders of the columns in a Pandas Dataframe. To show our Dataframe output in the terminal window in a table format, we are using tabulate package of Python. You can remove it if you do not want to use it and can use the default print() function of python to check the output of DataFrame in your console window.
Code Example
import pandas as pd
from tabulate import tabulate
students = {
"name": ["Tom", "John", "Rick", "Sneha", "Johnson"],
"score": [90, 80, 98, 99, 82],
"subjects": ["Math", "Physics", "Chemistry", "english", "Data Structure"]
}
df = pd.DataFrame(students)
print(tabulate(df, headers = 'keys', tablefmt = 'psql'))
df = df[["score", "name", "subjects"]]
print(tabulate(df, headers = 'keys', tablefmt = 'psql'))
Output
+----+---------+---------+----------------+
| | name | score | subjects |
|----+---------+---------+----------------|
| 0 | Tom | 90 | Math |
| 1 | John | 80 | Physics |
| 2 | Rick | 98 | Chemistry |
| 3 | Sneha | 99 | english |
| 4 | Johnson | 82 | Data Structure |
+----+---------+---------+----------------+
+----+---------+---------+----------------+
| | score | name | subjects |
|----+---------+---------+----------------|
| 0 | 90 | Tom | Math |
| 1 | 80 | John | Physics |
| 2 | 98 | Rick | Chemistry |
| 3 | 99 | Sneha | english |
| 4 | 82 | Johnson | Data Structure |
+----+---------+---------+----------------+
In the above code snippet, we have created a dictionary named students and using pd.DataFrame(students) we are generating a DataFrame and naming it df. To rearrange the DataFrame columns we are using the below code.
df[["score", "name", "subjects"]]
- Get column names from Pandas DataFrame as a python List
- Reorder dataframe columns using column names in pandas
- Add suffix/prefix to column names of Pandas DataFrame
- Pandas - Change rows order of a DataFrame using index list
- Change column values condition based in Pandas DataFrame
- Pandas - Delete,Remove,Drop, column from pandas DataFrame
- Change the name of columns in a pandas dataframe