- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to select all columns except one in a Pandas DataFrame?
To select all columns except one column in Pandas DataFrame, we can use df.loc[:, df.columns != <column name>].
Steps
Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
Print the input DataFrame, df.
Initialize a variable col with column name that you want to exclude.
Use df.loc[:, df.columns != col] to create another DataFrame excluding a particular column.
Print the DataFrame without col column.
Example
import pandas as pd df = pd.DataFrame( { "x": [5, 2, 1, 9], "y": [4, 1, 5, 10], "z": [4, 1, 5, 0] } ) print("Input DataFrame is:
", df) col = "y" df1 = df.loc[:, df.columns != col] print "DataFrame without Column-y:
", df1
Output
Input DataFrame is: x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0 DataFrame without Column-y: x z 0 5 4 1 2 1 2 1 5 3 9 0
- Related Articles
- Select multiple columns in a Pandas DataFrame
- Python - Select multiple columns from a Pandas dataframe
- How to select all rows from a table except the last one in MySQL?
- How to sort multiple columns of a Pandas DataFrame?
- Python - How to select a column from a Pandas DataFrame
- Python - How to select a subset of a Pandas DataFrame
- How to get list of all columns except one or more columns from an R data frame?
- Python Pandas - How to select multiple rows from a DataFrame
- Change Data Type for one or more columns in Pandas Dataframe
- Change Data Type for one or more columns in Pandas Dataframe
- Python - Grouping columns in Pandas Dataframe
- How to change the order of Pandas DataFrame columns?
- Python - Name columns explicitly in a Pandas DataFrame
- How to select multiple DataFrame columns using regexp and datatypes
- Python Pandas - Plot multiple data columns in a DataFrame?

Advertisements