Select all columns, except one given column in a Pandas DataFrame

DataFrame Data structure are the heart of Pandas library. Dataframes are basically two dimension Series object. They have rows and columns with rows representing the index and columns representing the content. Now, let’s see how to Select all columns, except one given column in Pandas Dataframe.

First, Let’s create a Dataframe:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library
import pandas as pd
  
# create a Dataframe
data = pd.DataFrame({
    'course_name': ['Data Structures', 'Python',
                    'Machine Learning'],
    'student_name': ['A', 'B'
                     'C'],
    'student_city': ['Chennai', 'Pune'
                     'Delhi'],
    'student_gender': ['M', 'F',
                       'M'] })
# show the Dataframe
data

chevron_right


Output:



DataFrame

DataFrame

Method 1: Using Dataframe.loc[ ].

This GeeksForGeeks Dataframe is just a two dimension array with numerical index. Therefore, to except only one column we could use the columns methods to get all columns and use a not operator to exclude the columns which are not needed. This method works only when the Dataframe is not multi indexed (did not have more than one index). 

Example: Select all columns, except one ‘student_gender’ column in Pandas Dataframe.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library
import pandas as pd
  
# create a Dataframe
data = pd.DataFrame({
    'course_name': ['Data Structures', 'Python',
                    'Machine Learning'],
    'student_name': ['A', 'B'
                     'C'],
    'student_city': ['Chennai', 'Pune'
                     'Delhi'],
    'student_gender': ['M', 'F',
                       'M'] })
                         
df = data.loc[ : , data.columns != 'student_gender']
  
# show the dataframe
df

chevron_right


Output:

filtered student_gender column

Method 2: Using drop() method.

Dataframe supports drop() method to drop a particular column. It accepts two arguments, column/row name and axis

Example: Select all columns, except one ‘student_city’ column in  Pandas Dataframe.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library
  
import pandas as pd
  
# create a Dataframe
data = pd.DataFrame({
   'course_name': ['Data Structures', 'Python',
                   'Machine Learning'],
  
   'student_name': ['A', 'B',
                    'C'],
  
   'student_city': ['Chennai', 'Pune',
                    'Delhi'],
  
   'student_gender': ['M', 'F',
                      'M'] })
  
# drop method
df = data.drop('student_city',
               axis = 1)
   
# show the dataframe
df

chevron_right


Output:

student_city column removed

student_city column removed

Method 3: Using Series.difference() method and [ ] operator together.

Series.difference() Method returns a new Index with elements from the index that are not in other.

Example: Select all columns, except one ‘student_name’ column in Pandas Dataframe.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library
import pandas as pd
  
# create a Dataframe
data = pd.DataFrame({
    'course_name': ['Data Structures', 'Python',
                    'Machine Learning'],
    'student_name': ['A', 'B'
                     'C'],
    'student_city': ['Chennai', 'Pune'
                     'Delhi'],
    'student_gender': ['M', 'F',
                       'M'] })
                         
df = data[data.columns.difference(['student_name'])]
  
# show the dataframe
df

chevron_right


Output:

filtered student_name column

filtered student_name column




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.