Being able to work with and manipulate Pandas DataFrames is an essential skill for anyone working in data analysis or data science. As Pandas becomes the ubiquitous tool for data analysts, being able to get DataFrames ready for presentation is important. In this tutorial, you’ll learn how to use Pandas to move a DataFrame column to a particular position, such as the start or end.
By the end of this tutorial, you’ll have learned the following:
- How to move a Pandas DataFrame column to the front
- How to move a Pandas DataFrame column to the end
- How to move a Pandas DataFrame column to a specific position
Table of Contents
Loading a Sample Pandas DataFrame
For the purposes of this tutorial, let’s load a sample Pandas DataFrame. The DataFrame will be deliberately simple and use numbered columns to give you a good sense of how the positions of columns is changing.
# Loading a Sample Pandas DataFrame
import pandas as pd
values = {i: [i] * 3 for i in range(4)}
df = pd.DataFrame(values)
print(df)
# Returns:
# 0 1 2 3
# 0 0 1 2 3
# 1 0 1 2 3
# 2 0 1 2 3
We can see that our DataFrame has four columns, each containing the values of their headings. We used a dictionary comprehension to simplify creating a dictionary of values.
Understanding the Pandas pop and insert Methods
Looking at how to move a Pandas DataFrame column using the .pop()
and .insert()
method:
Objective | Move a Pandas DataFrame Column |
pop() method | Extract a Pandas DataFrame column |
insert() method | Insert a Pandas DataFrame column at a specific position |
The Pandas .pop()
method accepts a column label as its argument. The method drops the column from the DataFrame and returns it.
The Pandas .insert()
method accepts a position, column label, and values to insert as its arguments. If this isn’t making sense just yet, don’t worry! Once we go through a few examples this will all become clear.
How to Move a Pandas DataFrame Column to the Front
To move a Pandas DataFrame column to the front of a DataFrame, first extract the column using the .pop()
method and then insert it at position 0 using the .insert()
method.
Let’s take a look at an example where we move the third column to the front of the DataFrame:
# Move a Pandas Column to the Front
import pandas as pd
values = {i: [i] * 3 for i in range(4)}
df = pd.DataFrame(values)
col2 = df.pop(2)
df.insert(0, '2', col2)
print(df)
# Returns:
# 2 0 1 3
# 0 2 0 1 3
# 1 2 0 1 3
# 2 2 0 1 3
Let’s break down how you can move a column to the front:
- We use the
.pop()
method to extract column 2 from the DataFrame and assign the Series tocol2
- We then use the
.insert()
method to insert the Series at position 0, with the label'2'
We can simplify this process into a single line of code by combining the two operations together, as shown below:
# Simplifying our code above
import pandas as pd
values = {i: [i] * 3 for i in range(4)}
df = pd.DataFrame(values)
df.insert(0, '2', df.pop(2))
print(df)
# Returns:
# 2 0 1 3
# 0 2 0 1 3
# 1 2 0 1 3
# 2 2 0 1 3
In the code above, we avoid having to use the .pop()
method on its own. Because the operation happens in place, we can use the method directly when calling the .insert()
method.
In the next section, you’ll learn how to move a Pandas DataFrame column to the last position.
How to Move a Pandas DataFrame Column to the End
In order to move a Pandas DataFrame column to the last position of the DataFrame, we can follow a very similar process to the one outlined above. We’ll make use of the .pop()
and .insert()
methods again.
When we use the .pop()
method, the DataFrame actually temporarily loses a column. Because of this, we need to be mindful of where we insert the column. Thankfully, we can count the number of columns and simply insert it using the len()
function.
Let’s see how we can move the first column to the end of a DataFrame
# Move a Column to the End of the DataFrame
import pandas as pd
values = {i: [i] * 3 for i in range(4)}
df = pd.DataFrame(values)
col0 = df.pop(0)
df.insert(len(df.columns), '0', col0)
print(df)
# Returns:
# 1 2 3 0
# 0 1 2 3 0
# 1 1 2 3 0
# 2 1 2 3 0
In the code above, we first use the .pop()
method to extract the column. We then insert it in the last position. In order to keep this code dynamic, we use the len()
function to count how many columns are in the DataFrame and insert it into that position.
We can simplify the code above, again, into a single method call. Because the DataFrame temporarily loses a column, we need to subtract 1 from its length:
df.insert(len(df.columns)-1, '0', df.pop(0))
Because the complexity of this code increases, it may be a good idea to use the longer version.
How to Move a Pandas DataFrame Column to a Specific Position
In this final section, we take a look at how we can move a Pandas DataFrame to a specific position. This method works in the same way as the examples above, expect we’re specifying a unique position.
Let’s take a look at how we can move the last DataFrame column to the second position. Because Python is 0-based index, this would be position 1
.
# Move a Column to a Specific Position
import pandas as pd
values = {i: [i] * 3 for i in range(4)}
df = pd.DataFrame(values)
df.insert(1, '3', df.pop(3))
print(df)
# Returns:
# 0 3 1 2
# 0 0 3 1 2
# 1 0 3 1 2
# 2 0 3 1 2
In the code above, we insert the popped last column into the second position. This can be simply chained into a single method call.
Conclusion
In this tutorial, you learned how to move a Pandas DataFrame column to a specific position, such as the beginning or the end. Being able to move columns around makes the readability of your data much better. This can be incredibly useful when you’re exporting data, such as into Excel files or CSV files.
You first learned how to use the .pop()
and .insert()
method to move a Pandas column to the front. Then, you learned how to replicate the process to move a column to the end of a DataFrame. Finally, you learned how to move a column to a specific position.
Additional Resources
To learn more about related topics, check out the tutorials below: