• Post author:
  • Post category:Pandas
  • Post last modified:September 25, 2024
  • Reading time:10 mins read
You are currently viewing Pandas – Set Order of Columns in DataFrame

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.

Advertisements

Key Points –

  • You can reorder columns by directly specifying the desired order in a list and assigning it back to the DataFrame.
  • The .loc[] accessor allows you to reorder columns by specifying the column names in the desired order.
  • With .iloc[], you can reorder columns by specifying their index positions, rather than their names.
  • The reindex() method allows reordering columns by passing a list of column names in the desired order, providing flexibility when working with existing DataFrames.
  • You can set the axis parameter to axis=1 to reorder columns when using the reindex() function.
  • If a column specified in reindex() does not exist in the DataFrame, it will create a new column with missing values (NaN) by default.

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.


# Quick examples of set order of columns in DataFrame

# 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.


# Create DataFrame
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.


# 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

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.


# 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

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.


# 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

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.


# 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

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.


# 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

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.


# 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

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 !!

References

Leave a Reply