You can use set order or rearrange columns of pandas DataFrame using either loc[]
, iloc[]
, and reindex()
methods. In this article, I will explain how to set the re-arrange the columns in a user-specific way with several examples.
1. Quick Examples of Set Order of Columns in DataFrame
If you are in a hurry, below are some quick examples of how to set the order of DataFrame columns.
# Below are quick example
# Set the columns in a order you want in a list and assign it to DF
df = df[['Courses','Fee','Discount','Duration']]
# Using DataFrame.loc[] to set column order
df2 = df.loc[:, ['Fee','Discount','Duration','Courses']]
# Using DataFrame.iloc[] to set column order by Index
df2 = df.iloc[:, [1,2,0,3]]
# Using DataFrame.reindex() to set columns order
columnsTitles = ['Courses','Discount','Fee','Duration']
df2 = df.reindex(columns=columnsTitles)
# Using DataFrame.reindex()
df2 = df.reindex(['Courses','Discount','Fee','Duration'], axis=1)
# set columns order using index
index = [0,2,1,3]
df2 = df[[df.columns[i] for i in index]]
Now, let’s create a DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names Courses
, Fee
, Duration
, and Discount
.
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Python","pandas"],
'Fee' :[20000,25000,22000,30000],
'Duration':['30days','40days','35days','50days'],
'Discount':[1000,2300,1200,2000]
}
df = pd.DataFrame(technologies)
print(df)
Yields below output.
Courses Fee Duration Discount
0 Spark 20000 30days 1000
1 PySpark 25000 40days 2300
2 Python 22000 35days 1200
3 pandas 30000 50days 2000
2. Set Order Columns in DataFrame Using []
You can set the order of the DataFrame columns in any way you want by specifying the columns in a list to df[]
, for example df[['Courses','Fee','Discount','Duration']]
.
# Rearrange the columns in custom order
df = df[['Courses','Fee','Discount','Duration']]
print(df.head())
# Change column Order Using []
df2 = df[['Courses','Fee','Discount','Duration']]
print(df2)
Yields below output.
Courses Fee Discount Duration
0 Spark 20000 1000 30days
1 PySpark 25000 2300 40days
2 Python 22000 1200 35days
3 pandas 30000 2000 50days
3. Set Order Columns Using DataFrame.loc[]
You can use df.loc[:, ['Fee','Discount','Duration','Courses']]
to set order of columns in pandas DataFrame. You need to assign this back to DataFrame to reflect the order.
# Using DataFrame.loc[] to set column order
df2 = df.loc[:, ['Fee','Discount','Duration','Courses']]
print(df2)
Yields below output.
Fee Discount Duration Courses
0 20000 1000 30days Spark
1 25000 2300 40days PySpark
2 22000 1200 35days Python
3 30000 2000 50days pandas
4.Rearrange Columns Using DataFrame.iloc[] with Index
You can also use DataFrame.iloc[]
the indexing syntax [:,[1,2,0,3]]
to re-arrange columns by Index in pandas DataFrame. If you are new to pandas refer Difference Between loc[] vs iloc[] to know more about using loc[] and iloc[].
# Using DataFrame.iloc[] to set column order
df2 = df.iloc[:, [1,2,0,3]]
print(df2)
Yields below output.
Fee Duration Courses Discount
0 20000 30days Spark 1000
1 25000 40days PySpark 2300
2 22000 35days Python 1200
3 30000 50days pandas 2000
5. Rearrange Columns Using DataFrame.reindex()
Use df.reindex(columns=columnsTitles)
with a list of columns in the desired order as columnsTitles
to set the columns.
# Using DataFrame.reindex() to set columns order
columnsTitles = ['Courses','Discount','Fee','Duration']
df2 = df.reindex(columns=columnsTitles)
print(df2)
# Using DataFrame.reindex()
df2 = df.reindex(['Courses','Discount','Fee','Duration'], axis=1)
print(df2)
Yields below output.
Courses Discount Fee Duration
0 Spark 1000 20000 30days
1 PySpark 2300 25000 40days
2 Python 1200 22000 35days
3 pandas 2000 30000 50days
6. Set Order Columns Using Index
You can also use df[[df.columns[i] for i in index]]
to set order columns in DataFrame.
# set columns order using index
index = [0,2,1,3]
df2 = df[[df.columns[i] for i in index]]
print(df2)
Yields below output.
Courses Duration Fee Discount
0 Spark 30days 20000 1000
1 PySpark 40days 25000 2300
2 Python 35days 22000 1200
3 pandas 50days 30000 2000
7. Complete Example to DataFrame Columns in Custom Order
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Python","pandas"],
'Fee' :[20000,25000,22000,30000],
'Duration':['30days','40days','35days','50days'],
'Discount':[1000,2300,1200,2000]
}
df = pd.DataFrame(technologies)
print(df)
# Now 'Duration' will appear at the end of our DataFrame
df = df[['Courses','Fee','Discount','Duration']]
print(df.head())
# Change column Order Using []
df2 = df[['Courses','Fee','Discount','Duration']]
print(df2)
# Using DataFrame.loc[] to set column order
df2 = df.loc[:, ['Fee','Discount','Duration','Courses']]
print(df2)
# Using DataFrame.iloc[] to set column order
df2 = df.iloc[:, [1,2,0,3]]
print(df2)
# Using DataFrame.reindex() to set columns order
columnsTitles = ['Courses','Discount','Fee','Duration']
df2 = df.reindex(columns=columnsTitles)
print(df2)
# Using DataFrame.reindex()
df2 = df.reindex(['Courses','Discount','Fee','Duration'], axis=1)
print(df2)
# set columns order using index
index = [0,2,1,3]
df2 = df[[df.columns[i] for i in index]]
print(df2)
Conclusion
In this article, you have learned how to set the order of columns in pandas DataFrame using DataFrame.loc[]
, DataFrame.iloc[]
and DataFrame.reindex()
methods with examples. Use these you can re-arrange the columns you want when you have a few columns and these approaches are not much feasible if you have hundreds of columns and would like to re-arrange.
Happy Learning !!
Related Articles
- How to Change Position of a Column in Pandas
- Change the Order of Pandas DataFrame Columns
- Sort Pandas DataFrame by Date (Datetime)
- Combine Two Series into pandas DataFrame
- Pandas Write DataFrame to CSV File
- Pandas DataFrame reindex() Function
- Pandas Sum DataFrame Rows With Examples
- Pandas Get Row Number of DataFrame