Pandas provide reindex()
, insert()
and select by columns to change the position of a DataFrame column, in this article, let’s see how to change the position of the last column to the first or move the first column to the end or get the column from middle to the first or last with examples.
Let’s create a DataFrame
# Create DataFrame
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark"],
'Fee' :[22000.30,25000.40],
'Duration':['30days','50days'],
'Discount':[1000.10,2300.15]
}
df = pd.DataFrame(technologies)
print(df)
Yields below output.
# Output:
Courses Fee Duration Discount
0 Spark 22000.3 30days 1000.10
1 PySpark 25000.4 50days 2300.15
1. Pandas Change Position of a Column (Last to the First)
You can change the position of a pandas column in multiple ways, the simplest way would be to select the columns by positioning the last column in the first position. You can also use this approach to change the order of pandas columns in the desired order.
# Move last Column to First Column
new_cols = ["Discount","Courses","Fee","Duration"]
df=df[new_cols]
# Or
df=df.reindex(columns=new_cols)
print(df)
Yields below output. This is not feasible if you have 100’s of columns.
# Output:
Discount Courses Fee Duration
0 1000.10 Spark 22000.3 30days
1 2300.15 PySpark 25000.4 50days
Alternatively, you can also try selecting columns by indexes.
# Move last column to the first
df = pd.DataFrame(technologies)
temp_cols=df.columns.tolist()
new_cols=temp_cols[-1:] + temp_cols[:-1]
df=df[new_cols]
print(df)
pandas also provide DataFrame.insert()
method to insert a column into DataFrame at the specified location. To use this, you need to know the column names you would like to move.
# If you know the column name
df = pd.DataFrame(technologies)
col = df.pop("Discount")
df = df.insert(0, col.name, col)
print(df)
2. Move the First Column to the Last in pandas DataFrame
Now let’s see how to move the first column to the last position in pandas DataFrame.
# Move first column to the Last
df = pd.DataFrame(technologies)
temp_cols=df.columns.tolist()
new_cols=temp_cols[1:] + temp_cols[0:1]
df=df[new_cols]
print(df)
Yields below output.
# Output:
Fee Duration Discount Courses
0 22000.3 30days 1000.10 Spark
1 25000.4 50days 2300.15 PySpark
3. Move Middle Column to the Begining or Ending of the DataFrame
Moving first to last and last to first is simple, now let’s see moving the middle column to the first position of the DataFrame.
# Move Middle column to the Begining
df = pd.DataFrame(technologies)
temp_cols=df.columns.tolist()
index=df.columns.get_loc("Duration")
new_cols=temp_cols[index:index+1] + temp_cols[0:index] + temp_cols[index+1:]
df=df[new_cols]
print(df)
Yields below output.
# Output:
Duration Courses Fee Discount
0 30days Spark 22000.3 1000.10
1 50days PySpark 25000.4 2300.15
I will leave it to you to explore moving the middle column to the last position.
Happy Learning !!
Related Articles
- Pandas How to Rename DataFrame Columns?
- Pandas How to Add a new Column to DataFrame?
- Pandas How to Drop single or multiple columns?
- Pandas Get Column Name by Index or Position
- Get First N Rows of Pandas DataFrame
- Pandas Groupby Aggregate Explained
- Pandas Get Last Row from DataFrame?
Remove `df = ` in
“`
# if you know the column name
df = pd.DataFrame(technologies)
col = df.pop(“Discount”)
df = df.insert(0, col.name, col)
print(df)
“`