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:
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.
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.
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.
- How to select multiple columns in a pandas dataframe
- Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc
- Split a text column into two columns in Pandas DataFrame
- Create a new column in Pandas DataFrame based on the existing columns
- How to Select single column of a Pandas Dataframe?
- Get column index from column name of a given Pandas DataFrame
- Convert given Pandas series into a dataframe with its index as another column on the dataframe
- Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
- How to drop one or multiple columns in Pandas Dataframe
- Change Data Type for one or more columns in Pandas Dataframe
- Count the NaN values in one or more columns in Pandas DataFrame
- How to select rows from a dataframe based on column values ?
- Loop or Iterate over all or certain columns of a dataframe in Python-Pandas
- Create a DataFrame from a Numpy array and specify the index column and column headers
- Join two text columns into a single column in Pandas
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- How to rename columns in Pandas DataFrame
- Difference of two columns in Pandas dataframe
- Getting frequency counts of a columns in Pandas DataFrame
- Dealing with Rows and Columns in Pandas DataFrame
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.