How to change the order of DataFrame columns?
To change the order of DataFrame columns in Pandas, you can use either indexing or the reindex() method. By specifying the desired order of column names, you can rearrange the columns accordingly. Additionally, you can use the loc indexer to reorder the columns based on their labels.
First let's create a data frame with values.
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['Name'] = ['John', 'Doe', 'Bill','Jim','Harry','Ben']
df['TotalMarks'] = [82, 38, 63,22,55,40]
df['Grade'] = ['A', 'E', 'B','E','C','D']
df['Promoted'] = [True, False,True,False,True,True]
df
Name TotalMarks Grade Promoted
0 John 82 A True
1 Doe 38 E False
2 Bill 63 B True
3 Jim 22 E False
4 Harry 55 C True
5 Ben 40 D True
Print the column in order
cols = df.columns.tolist()
cols
['Name', 'TotalMarks', 'Grade', 'Promoted']
Rearrange the column order manually
df =df[['Promoted','Grade','TotalMarks','Name']]
Again print the column order:
cols = df.columns.tolist()
cols
['Promoted', 'Grade', 'TotalMarks', 'Name']
Change column order using index
Following program change the last colun first using column index.
cols = df.columns.tolist()
cols
['Name', 'TotalMarks', 'Grade', 'Promoted']
Change the last column to first
cols = df.columns.tolist()
cols = cols[-1:] + cols[:-1]
df =df[cols]
df
Promoted Name TotalMarks Grade
0 True John 82 A
1 False Doe 38 E
2 True Bill 63 B
3 False Jim 22 E
4 True Harry 55 C
5 True Ben 40 D
Reorder DataFrame column in sorted order
df = df.reindex(columns=sorted(df.columns))
df
Grade Name Promoted TotalMarks
0 A John True 82
1 E Doe False 38
2 B Bill True 63
3 E Jim False 22
4 C Harry True 55
5 D Ben True 40
Using for loop to rearrange column order
To rearrange the column order in a Pandas DataFrame using a for loop, you can create a list containing the desired order of column names and then use the list to reassign the columns of the DataFrame.
cols = ['Grade'] + [col for col in df if col != 'Grade']
df = df[cols]
df
Grade Name TotalMarks Promoted
0 A John 82 True
1 E Doe 38 False
2 B Bill 63 True
3 E Jim 22 False
4 C Harry 55 True
5 D Ben 40 True
Conclusion
Both methods allow you to customize the column order based on your requirements.
Related Topics
- Creating an empty Pandas DataFrame
- How to Check if a Pandas DataFrame is Empty
- How to check if a column exists in Pandas Dataframe
- How to delete column from pandas DataFrame
- How to select multiple columns from Pandas DataFrame
- Selecting multiple columns in a Pandas dataframe based on condition
- Selecting rows in pandas DataFrame based on conditions
- How to Drop rows in DataFrame by conditions on column values
- Rename column in Pandas DataFrame
- Get a List of all Column Names in Pandas DataFrame
- How to add new columns to Pandas dataframe?
- Concatenate two columns into a single column in pandas dataframe
- How to count the number of rows and columns in a Pandas DataFrame
- Use a list of values to select rows from a pandas dataframe
- How to iterate over rows in a DataFrame in Pandas
- How to drop rows/columns of Pandas DataFrame whose value is NaN
- How to Export Pandas DataFrame to a CSV File
- Convert list of dictionaries to a pandas DataFrame
- How to set a particular cell value in pandas DataFrame