In This Section we will be focusing on how to move a column to first position in pandas dataframe and also how to move a column to the last position in pandas dataframe. There are multiple ways to do it, like pop() and insert() function, we will also move multiple columns to start and end position of the dataframe in pandas python. let’s look at each of these cases in pandas with an example for each.
- Move an existing column to start of the dataframe pandas python (move to first position ).
- Move an existing column to end of the dataframe pandas python (move to last position).
- Move multiple columns to start of the dataframe pandas python (move to starting positions).
- Move multiple columns to end of the dataframe pandas python (move to ending positions).
Create Dataframe:
## create dataframe import pandas as pd import numpy as np #Create a DataFrame import pandas as pd import numpy as np d = { 'Name':['Alisa','Bobby','Cathrine','Jodha','Raghu','Ram'], 'Age':[26,23,23,23,23,24], 'Score':[85,31,55,55,31,77], 'City':['Newyork','Seattle','Toronto','California','Delhi','Mumbai']} df = pd.DataFrame(d,columns=['Name','Age','Score','City']) df
The Resultant dataframe is
Move an existing column to start of the dataframe pandas python: Method 1
Using pop() function to pop the column directly . In General pop() function pops the last element every time. But in the pandas pop method can take input of a column from a data frame and pop that column directly and then insert() function is used to move the popped column to first position as shown
#### move a column to start of the dataframe first_column = df.pop('Age') df.insert(0, 'Age', first_column) df
So in the above example “Age” column has been popped and inserted as a first column , so the resultant dataframe will be
Move an existing column to start of the dataframe pandas python: Method 2
An another easy method is select the column to be moved to first position and rest of the column will be provided as a list excluding the already selected column as shown below.
#### move a column to start of the dataframe df = df[['Age'] + [ col for col in df.columns if col != 'Age' ]] df
So in the “Age” column will be moved first and the rest of the columns will be as such so the resultant dataframe will be
Move an existing column to End of the dataframe pandas python: Method 1
Using pop() function to pop the column directly, in the pandas pop method can take input of a column from a data frame and pop that column directly and then insert() function is used to move the popped column to last position as shown
#### move a column to end of the dataframe last_column = df.pop('Age') df.insert(df.shape[1], 'Age', last_column) df
or
#### move a column to end of the dataframe last_column = df.pop('Age') df.insert(len(df.columns), 'Age', last_column) df
So in the above example “Age” column has been popped and inserted as a last column , so the resultant dataframe will be
Move an existing column to End of the dataframe pandas python: Method 2
An another easy method is select the column to be moved to Last position and rest of the column will be provided as a list excluding the already selected column as shown below.
#### move a column to end of the dataframe : Method 2 df = df[[ col for col in df.columns if col != 'Age' ] + ['Age']] df
So in the “Age” column will be moved last and the rest of the columns will be as such so the resultant dataframe will be
Move multiple columns to start of the dataframe pandas python
An easy method is to select the list column to be moved to First positions and rest of the column will be provided as a list excluding the already selected list of columns as shown below.
##### Move multiple columns to the start of the dataframe : Method 1 cols_to_move = ['Age', 'Score'] df= df[ cols_to_move + [ col for col in df.columns if col not in cols_to_move ]] df
So the “Age” and “Score” column will be moved to First and second position and the rest of the columns will be as such, so the resultant dataframe will be
Move multiple columns to End of the dataframe pandas python
An easy method is to select the list column to be moved to Last position and rest of the column will be provided as a list excluding the already selected list of columns as shown below.
##### Move multiple columns to the End of the dataframe : Method 1 cols_to_move = ['Age', 'Score'] df= df[[ col for col in df.columns if col not in cols_to_move ] + cols_to_move] df
So in the “Age” and “Score” column will be moved to last two positions and the rest of the columns will be as such so the resultant dataframe will be