In this article, we will discuss different ways to delete last N columns of a dataframe in python.
Use iloc to drop last N columns of pandas dataframe
In Pandas, the Dataframe provides an attribute iloc to select a portion of the dataframe using position based indexing. This selected portion can be a few columns or rows . We can use this attribute to select all the columns except last N columns of the dataframe and then assign back that to the original variable. It will give an effect that we have deleted the last N columns from the dataframe. For example,
N = 3 # Drop last N columns of dataframe df = df.iloc[: , :-N]
We selected a portion of dataframe, that included all rows, but it selected only first N-1 columns. Then assigned this back to the same variable. So, basically it removed the last N columns of dataframe.
How did it work?
The syntax of dataframe.iloc[] is like,
df.iloc[row_start:row_end , col_start, col_end]
- row_start: The row index/position from where it should start selection. Default is 0.
- row_end: The row index/position from where it should end the selection i.e. select till row_end-1. Default is till the first row of the dataframe.
- col_start: The column index/position from where it should start selection. Default is 0.
- col_end: The column index/position from where it should end the selection i.e. select till col_end-1. Default is till the first column of the dataframe.
It returns a portion of dataframe that includes rows from row_start to row_end-1 and columns from col_start to col_end-1.
To delete the last N columns of the dataframe, just select the columns from column number 0 till -N (i.e. N less from last) and select all rows. To select all the rows use default values i.e. (:) i.e. Drop last N columns of dataframe
Frequently Asked:
- How to convert dtype ‘object’ to int in Pandas?
- Pandas: Delete last column of dataframe in python
- Pretty Print a Pandas Dataframe
- Pandas Tutorial #15 – Merging DataFrames
df = df.iloc[: , :-N]
Checkout complete example to delete the last 3 columns of dataframe,
import pandas as pd # List of Tuples empoyees = [('Jack', 34, 'Sydney', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aadi', 16, 'London', 11) , ('Mark', 41, 'Delhi' , 12)] # Create a DataFrame object df = pd.DataFrame( empoyees, columns=['Name', 'Age', 'City', 'Experience']) print("Contents of the Dataframe : ") print(df) N = 3 # Drop last N columns of dataframe df = df.iloc[: , :-N] print("Modified Dataframe : ") print(df)
Output:
Contents of the Dataframe : Name Age City Experience 0 Jack 34 Sydney 5 1 Riti 31 Delhi 7 2 Aadi 16 London 11 3 Mark 41 Delhi 12 Modified Dataframe : Name 0 Jack 1 Riti 2 Aadi 3 Mark
Use drop() to remove last N columns of pandas dataframe
In pandas, the dataframe’s drop() function accepts a sequence of column names that it needs to delete from the dataframe. To make sure that it removes the columns only, use argument axis=1 and to make changes in place i.e. in calling dataframe object, pass argument inplace=True.
Checkout complete example to delete the last 3 columns of dataframe,
import pandas as pd # List of Tuples empoyees = [('Jack', 34, 'Sydney', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aadi', 16, 'London', 11) , ('Mark', 41, 'Delhi' , 12)] # Create a DataFrame object df = pd.DataFrame( empoyees, columns=['Name', 'Age', 'City', 'Experience']) print("Contents of the Dataframe : ") print(df) N = 3 # Drop last N columns of dataframe df.drop(columns=df.columns[-N:], axis=1, inplace=True) print("Modified Dataframe : ") print(df)
Output:
Contents of the Dataframe : Name Age City Experience 0 Jack 34 Sydney 5 1 Riti 31 Delhi 7 2 Aadi 16 London 11 3 Mark 41 Delhi 12 Modified Dataframe : Name 0 Jack 1 Riti 2 Aadi 3 Mark
We fetched the column names of dataframe as a sequence and passed the last N column names ( df.columns[-N:] ) as the columns argument in drop() function, therefore it deleted the last N columns (3 columns) of dataframe.
Use pop() to remove last N columns of pandas dataframe
In Pandas, dataframe provides a function pop(column_name). It expects a column name as an argument and deletes that column from the calling dataframe object. It also returns the deleted column as a series. We can use this to delete last N column of dataframe, for that we need to iterate over column names of dataframe in reverse and for first N columns call pop() function one by one. For example,
import pandas as pd # List of Tuples empoyees = [('Jack', 34, 'Sydney', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aadi', 16, 'London', 11) , ('Mark', 41, 'Delhi' , 12)] # Create a DataFrame object df = pd.DataFrame( empoyees, columns=['Name', 'Age', 'City', 'Experience']) print("Contents of the Dataframe : ") print(df) N = 3 # Drop last N columns of dataframe for i in range(N): df.pop(df.columns.values[-1]) print("Modified Dataframe : ") print(df)
Output:
Contents of the Dataframe : Name Age City Experience 0 Jack 34 Sydney 5 1 Riti 31 Delhi 7 2 Aadi 16 London 11 3 Mark 41 Delhi 12 Modified Dataframe : Name 0 Jack 1 Riti 2 Aadi 3 Mark
It removed the last 3 columns of dataframe in place.
Use del keyword to remove last N columns of pandas dataframe
Iterate over last N column names of dataframe and for each of them select the column by passing column name in subscript operator i.e. df[df.columns[-1]]. Then call del keyword on that selected column.
Checkout complete example to remove the last 3 columns of dataframe,
import pandas as pd # List of Tuples empoyees = [('Jack', 34, 'Sydney', 5) , ('Riti', 31, 'Delhi' , 7) , ('Aadi', 16, 'London', 11) , ('Mark', 41, 'Delhi' , 12)] # Create a DataFrame object df = pd.DataFrame( empoyees, columns=['Name', 'Age', 'City', 'Experience']) print("Contents of the Dataframe : ") print(df) N = 3 # Drop last N columns of dataframe for i in range(N): del df[df.columns.values[-1]] print("Modified Dataframe : ") print(df)
Output:
Contents of the Dataframe : Name Age City Experience 0 Jack 34 Sydney 5 1 Riti 31 Delhi 7 2 Aadi 16 London 11 3 Mark 41 Delhi 12 Modified Dataframe : Name 0 Jack 1 Riti 2 Aadi 3 Mark
It deleted the last 3 columns of dataframe in place.
Summary:
We learned about four different ways to delete last N columns of a dataframe.