Select all columns except one column in Pandas

In this article, we will discuss different ways to select all columns of a Pandas dataFrame except one specified column.

Table Of Contents

what do you mean by DataFrame in pandas?

DataFrame is a two-dimensional object which contains columns and rows. Where columns represent the content and rows representing the index. DataFrame is like a tabular data structure.

Problem Statement

Suppose we have a DataFrame,

   RollNo student_name student_city    student_Email
0      10        Reema        Surat  [email protected]
1      20        Rekha         Pune  [email protected]
2      30         Jaya        Delhi   [email protected]

We want to select all columns from the DataFrame except one column i.e. ‘student_city’. Like this,

   RollNo student_name    student_Email
0      10        Reema  [email protected]
1      20        Rekha  [email protected]
2      30         Jaya   [email protected]

There are different ways select all columns except one column in pandas. Let’s discuss them one by one.

Select all columns except one using DataFrame.loc[]

A Pandas DataFrame is two-dimension data structure with the numeric index. So, to exclude one column from the DataFrame, we can use the NOT operator with DataFrame.loc[] method. This method works on single indexed DataFrame.

Syntax of DataFrame.loc[]

DataFrame.loc[row_section: column_section]

It returns a subset of DataFrame based on the row and column names in the row_section and column_section.

A pandas Script to select all columns except the ‘Student_city’ column from the DataFrame. It contains four columns ‘RollNo’,’Student_name’,’Student_city’ and ‘Student_email’. Let’s see how to select all columns except the column ‘Student_city’ using dataFrame.loc() method

import pandas as pd

# create a Dataframe
df = pd.DataFrame({
    'RollNo': [10, 20, 30],
    'student_name': ['Reema', 'Rekha', 'Jaya'],
    'student_city': ['Surat', 'Pune', 'Delhi'],
    'student_Email': ['[email protected]', '[email protected]', '[email protected]'] })

# Show the Dataframe
print(df)

print('****************')

# Select all columns except column 'student_city' from Dataframe
newDf = df.loc[ : , df.columns != 'student_city']

# show the Dataframe
print(newDf)

Output

   RollNo student_name student_city    student_Email
0      10        Reema        Surat  [email protected]
1      20        Rekha         Pune  [email protected]
2      30         Jaya        Delhi   [email protected]

****************

   RollNo student_name    student_Email
0      10        Reema  [email protected]
1      20        Rekha  [email protected]
2      30         Jaya   [email protected]

In the above script, we have first created DataFrame with four columns named ‘RollNo’, ‘Student_name’,’Student_city’,’Student_email’ using pandas DataFrame method. After creating DataFrame we have use dataFrame.loc[] method with NOT operator (!=) to exclude one column ‘Student_city’ from the selection. Basically in the DataFrame.loc[row_section: column_section] we have passed all columns except column student_city.

Select all columns except one using DataFrame.drop()

DataFrame.drop() method is used to drop a specific column from the DataFrame. It accepts two arguments, the first argument is the column/row name and the second is the axis. If the value of axis is 1, then it means drop() function will consider the first argument as column name and it will return a copy of the calling DataFrame object after deleting the specified column from that. So, df.drop(‘student_city’, axis = 1) will return a new DataFrame that will have all the columns except the column student_city.

A pandas Script to select all columns except ‘Student_city’ column from four columns ‘RollNo’,’Student_name’,’Student_city’ and ‘Student_email’, using the dataFrame.drop() method is as follows,

import pandas as pd

# create a Dataframe
df = pd.DataFrame({
    'RollNo': [10, 20, 30],
    'student_name': ['Reema', 'Rekha', 'Jaya'],
    'student_city': ['Surat', 'Pune', 'Delhi'],
    'student_Email': ['[email protected]', '[email protected]', '[email protected]'] })

# Show the Dataframe
print(df)

print('****************')

# Select all columns except column 'student_city' from Dataframe
newDf = df.drop('student_city', axis = 1)

# show the Dataframe
print(newDf)

Output

   RollNo student_name student_city    student_Email
0      10        Reema        Surat  [email protected]
1      20        Rekha         Pune  [email protected]
2      30         Jaya        Delhi   [email protected]

****************

   RollNo student_name    student_Email
0      10        Reema  [email protected]
1      20        Rekha  [email protected]
2      30         Jaya   [email protected]

In the above script, we have used the dataFrame.drop() method to select all columns from DataFrame except one column.

Select all columns except one using series.difference()

In Pandas, the series.difference() method returns a different index object. It removes the specifed column name from the column names and returns remaining columns. Basically, it calculates the difference of a Series element compared with another element in the Series.

Write pandas Script to select all coulmns except ‘Student_city’ column from DataFrame contains four columns ‘RollNo’,’Student_name’,’Student_city’ and ‘Student_email’ using Series.difference() method.

import pandas as pd

# create a Dataframe
data = pd.DataFrame({
    'RollNo': [10, 20, 30],
    'student_name': ['Reema', 'Rekha', 'Jaya'],
    'student_city': ['Surat', 'Pune', 'Delhi'],
    'student_Email': ['[email protected]', '[email protected]', '[email protected]'] })

# show the Dataframe
print(data)

print('**********')

# Exclude one column from Dataframe
df = data[data.columns.difference(['student_city'])]

# Show the Dataframe
print(df)

Output

   RollNo student_name student_city    student_Email
0      10        Reema        Surat  [email protected]
1      20        Rekha         Pune  [email protected]
2      30         Jaya        Delhi   [email protected]

**********

   RollNo    student_Email student_name
0      10  [email protected]        Reema
1      20  [email protected]        Rekha
2      30   [email protected]         Jaya

In the above script, we have used series.difference() to remove a column from the DataFram.

Select all columns except one using isin() method with Bitwise NOT operator

In Pandas, the isin() method is used for selecting a particular row or columns from dataFrame. It filters the dataFrame using Bit-wise NOT operator (~). With the isin() method we can filter a particular column from dataFrame and also exclude the same column.

A pandas Script to select all coulmns except ‘Student_city’ column from DataFrame contains four columns ‘RollNo’,’Student_name’,’Student_city’ and ‘Student_email’ using isin() method is as follows,

import pandas as pd

# create a Dataframe
df = pd.DataFrame({
        'RollNo': [10, 20, 30],
        'student_name': ['Reema', 'Rekha', 'Jaya'],
        'student_city': ['Surat', 'Pune', 'Delhi'],
        'student_Email': ['[email protected]', '[email protected]', '[email protected]'] })

# show the Dataframe
print(df)

print('**********')

# Exclude one column from Dataframe
df = df.loc[:, ~df.columns.isin(['student_city'])]

# show the Dataframe
print(df)

Output

   RollNo student_name student_city    student_Email
0      10        Reema        Surat  [email protected]
1      20        Rekha         Pune  [email protected]
2      30         Jaya        Delhi   [email protected]

**********

   RollNo student_name    student_Email
0      10        Reema  [email protected]
1      20        Rekha  [email protected]
2      30         Jaya   [email protected]

In the above script, the isin() method is used with negate operator i.e. Bitwise NOT to exclude one column from dataFrame, and select all other columns.

Summary

In the article, we learned to select all columns except one column from a DataFrame. We have discussed an overview of DataFrame, also list out four methods to select all columns from the dataFrame excluding one column with practical examples. Happy Coding.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top