DataFrames in Pandas is the 2-dimensional data structure containing information about rows and columns. DataFrames are utilized for computing large datasets for data analysis or handling the missing values. However, in data wrangling, there is a need to drop the DataFrame columns to handle the missing values or change the order of DataFrame Columns in Python. Doing so, to get useful insights from the DataFrame in Python.
This article will demonstrate changing the order of DatatFrame columns in Python. In this context, the articles covers the following topics:
- How to Change the Order of DataFrame Columns in Python?
- Re-order the DataFrame Columns in Python Using the “iloc” approach
- Change the Order of DataFrame Columns in Python Using the “loc” Function
- Double Brackets Practice to Re-order the DataFrame Columns in Python
- Using the “Reverse()” Function to Re-order the DataFrame Columns in Python
- Using the “reindex()” Function to Re-order the DataFrame Columns in Python
- Using a List Comprehension to Change the Order of the DataFrame Columns in Python
How to Change the Order of DataFrame Columns in Python?
To handle data and manipulate data according to the need, the Pandas DataFrame provides flexible access to programmers. However, most of the time, for data analysis, there is a need to change the order of DataFrame columns. However, by utilizing the straightforward standard built-in Pandas functions you can change the order of DataFrame columns in Python. To do so, follow the below set of approaches:
Approach 1: Re-order the DataFrame Columns in Python Using the “iloc” Approach
To manipulate data in the DataFrame Columns in Pandas Python, use the “iloc” method. By specifying the selected columns in the “iloc” function, you can change the order of DataFrame columns in Python. Here’s how you can change the DatFrame columns in Python:
- Consider creating a DataFrame or loading a Dataframe into the script.
- The “iloc” method is required to select the columns to change the order of DataFrame columns. To do so, specify the order of DataFrame columns within the “iloc” list as [1,2,0]. The “:”, represents that all the rows from the DataFrame are included.
# importing the libraries
import pandas as pd
# construct the DataFrame
data= {'city': ['?stanbul','?zmir','Antalya' ],
'population': ['16 million','4.4 million', '870,000' ],
'famous for': ['The Blue Mosque', 'Museum', 'Hadrians Gate' ]}
df = pd.DataFrame(data = data)
# printing the original DataFrame
print("Original DataFrame:\n")
print(df)
# printing the new DataFrame
print("\n\nModified DataFrame:\n")
modified_df=df.iloc[:,[1,2,0]]
print(modified_df)
The above example demonstrates utilizing the “iloc” method to change the order of DataFrame columns in Python. For that purpose, the order of the DataFrame columns is specified within the “iloc” square brackets “[ ]”.
Output
Approach 2: Change the Order of DataFrame Columns in Python Using “loc” Function
To change the order of DataFrame columns in Python, use the “loc” method. By specifying the “column names” directly within the square brackets “[ ]” you can change the order of DataFrame columns in Python. Here’s how you can change the DataFrame columns order in Pandas Python:
- Within the “loc” brackets specify the row selection then after the comma (,) separator, specify the order of the columns within the list “[ ]”. The “:” depicts that all the rows are included and within the inner list “[ ]” specifies that the column names labels to which you want to change their order:
# importing the modules
import pandas as pd
# construct the DataFrame
data= {'city': ['?stanbul','?zmir','Antalya' ],
'population': ['16 million','4.4 million', '870,000' ],
'famous for': ['The Blue Mosque', 'Museum', 'Hadrians Gate' ]}
df = pd.DataFrame(data = data)
# printing the original DataFrame
print("Original DataFrame:\n")
print(df)
# printing the new DataFrame
print("\n\nModified DataFrame:\n")
modified_df=df.loc[:,['famous for','population','city']]
print(modified_df)
Access the “loc” function with the DataFrame using the “dot (.)” notation. After accessing the dataframe now you can manipulate the DataFrame. To change the order of DataFrame Columns specify the column name labels within the square brackets “[ ]”.
Output
Approach 3: Double Brackets Practice to Re-order the DataFrame Columns in Python
Another approach to reordering the DataFrame columns is to specify the order of DataFrame columns in Python. For this purpose, the DataFrame column name labels are introduced in the directory in the DataFrame brackets. Here’s how you can understand the idea of reordering the DataFrame columns in Python:
# importing the modules
import pandas as pd
# construct the DataFrame
data= {'city': ['?stanbul','?zmir','Antalya' ],
'population': ['16 million','4.4 million', '870,000' ],
'famous for': ['The Blue Mosque', 'Museum', 'Hadrians Gate' ]}
df = pd.DataFrame(data = data)
# printing the original DataFrame
print("Original DataFrame:\n")
print(df)
# printing the new DataFrame
print("\n\nModified DataFrame:\n")
modified_df=df[['famous for','population','city']]
print(modified_df)
The double bracket “df[[]]” specifies that all the columns that are included within them are considered. However, the order of the DataFrame columns is specified within the inner brackets “[]” of the DataFrame.
Output
Approach 4: Using the “Reverse()” Function to Re-order the DataFrame Columns in Python
Another practice to change the order of DataFrame columns in Python is to utilize the reverse() function. First, get the list of DataFrame columns by using the “list” function. Within the “list” function specify the “columns” attribute to access the DataFrame columns using the “dot (.)” notation.
After retrieving the list of DataFrame Columns, invoke the “reverse()” function. The “reverse()” function will reverse the order of DataFrame columns in Python. The “df[columns]” syntax contains the columns that are in reverse order. Here’s how you can implement it through the following code example:
# importing the modules
import pandas as pd
# construct the DataFrame
data= {'city': ['?stanbul','?zmir','Antalya' ],
'population': ['16 million','4.4 million', '870,000' ],
'famous for': ['The Blue Mosque', 'Museum', 'Hadrians Gate' ]}
df = pd.DataFrame(data = data)
# printing the original DataFrame
print("Original DataFrame:\n")
print(df)
# printing the new DataFrame
print("\n\nModified DataFrame:\n")
columns = list(df.columns)
columns.reverse()
modified_df=df[columns]
print(modified_df)
Output
Approach 5: Using the “reindex()” Function to Re-order the DataFrame Columns in Python
The prevalent approach to changing the order of DataFrame columns in Python uses the reindex() function. The “reindex()” function will change the order of Pandas DataFrame. For demonstration follow the below example:
# importing the modules
import pandas as pd
# construct the DataFrame
data= {'city': ['?stanbul','?zmir','Antalya' ],
'population': ['16 million','4.4 million', '870,000' ],
'famous for': ['The Blue Mosque', 'Museum', 'Hadrians Gate' ]}
df = pd.DataFrame(data = data)
# printing the original DataFrame
print("Original DataFrame:\n")
print(df)
# printing the new DataFrame
print("\n\nModified DataFrame:\n")
modified_df=df.reindex(['famous for','population','city'], axis=1)
print(modified_df)
Within the reindex() function, specify the order of the columns within the square brackets “[ ]” and choose the target on which you want to make changes. To access the columns, set the “axis” parameter to “1”.
Output
Another practice to use the reindex() function for changing the order of DataFrame in Python is by sorting the DataFrame columns in either ascending or descending order in Python. However, to order the DataFrame Columns in alphabetically ascending order, utilize the reindex() function by specifying the target you want to act on. Here’s how you can sort the DataFrame columns in Python:
# importing the modules
import pandas as pd
# construct the DataFrame
data= {'city': ['?stanbul','?zmir','Antalya' ],
'population': ['16 million','4.4 million', '870,000' ],
'famous for': ['The Blue Mosque', 'Museum', 'Hadrians Gate' ]}
df = pd.DataFrame(data = data)
# printing the original DataFrame
print("Original DataFrame:\n")
print(df)
# printing the new DataFrame
print("\n\nModified DataFrame:\n")
modified_df= df.reindex(columns=sorted(df.columns))
print(modified_df)
To change the order of DataFrame columns in Python using the reindex() function, you need the target on which you want to perform the re-order operation. To do so, within the parentheses of the reindex() function, perform the sort operation on the “columns” parameter. With the assignment operator (=), using the sorted() function and “columns” attribute, access the “DataFrame” with the dot (.) operator.
Output
The “sorted(df.columns)” will change the order of DataFrame Columns in Python in ascending order:
Approach 6: Using a List Comprehension to Change the Order of the DataFrame Columns in Python
By utilizing the list comprehension one-liner if else statement approach you can change the order of DataFrame columns in Python. To do so, within the brackets of DataFrame perform two operations on the DataFrame, one is concatenation and the other is list-comprehension. Here’s how you can operate to change the order of DataFrame Columns:
# importing the modules
import pandas as pd
# construct the DataFrame
data= {'city': ['?stanbul','?zmir','Antalya' ],
'population': ['16 million','4.4 million', '870,000' ],
'famous for': ['The Blue Mosque', 'Museum', 'Hadrians Gate' ]}
df = pd.DataFrame(data = data)
# printing the original DataFrame
print("Original DataFrame:\n")
print(df)
# printing the new DataFrame
print("\n\nModified DataFrame:\n")
#df = df.columns.tolist()
# Rearrange the list any way you want
#modified_df = df[-1:] + df[:-1]
modified_df = df[ ['famous for'] + [ col for col in df.columns if col != 'famous for']]
print(modified_df)
The list comprehension structure will iterate through the DataFrame columns and select all the columns except the one column that is excluded through the augmented assignment operator (!=). The “[‘famous for’]” will excluded from the list of other DataFrame columns. The new list will consider the “[‘famous for’]” columns placed at the index location 0, and then other columns following their pre-specified order in DataFrame.
Output
List Comprehension without the “if” Statement
To change the order of DataFrame columns in Python, utilize them for…in the structure of list comprehension to access the DataFrame column’s position. To do so, you need to specify the order of the columns in the list and save them in the variable first. Then, within the double brackets of the DataFrame, iterate the DataFrames columns over the list elements specified in the “order” variable.
Here’s how you can perform the DataFrame Columns re-ordering operation:
# importing the modules
import pandas as pd
# construct the DataFrame
data= {'city': ['?stanbul','?zmir','Antalya' ],
'population': ['16 million','4.4 million', '870,000' ],
'famous for': ['The Blue Mosque', 'Museum', 'Hadrians Gate' ]}
df = pd.DataFrame(data = data)
# printing the original DataFrame
print("Original DataFrame:\n")
print(df)
# printing the new DataFrame
print("\n\nModified DataFrame:\n")
order = [2,1,0] # setting column's order
modified_df = df[[df.columns[i] for i in order]]
print(modified_df)
Output
That’s all about changing the order of DataFrame columns in Python.
Conclusion
To change the order of DataFrame columns in Python, use the “iloc” or “loc” function by specifying the order of the columns within their brackets. Moreover, utilize the reverse() or reindex() function to re-order the DataFrame Columns. Additionally, utilize the list comprehension approach to access the DataFrame columns. This article demonstrates changing the order of DataFrame columns in Python.