
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a program in Python to remove one or more than one columns in a given DataFrame
Assume, you have a dataframe,
one two three 0 1 2 3 1 4 5 6
And the result for removing single column is,
two three 0 2 3 1 5 6
The result for removing after more than one column is,
three 0 3 1 6
To solve this, we will follow the steps given below −
Solution 1
Define a dataframe
Delete a particular column using below method,
del df['one']
Example
Let’s see the below code to get a better understanding −
import pandas as pd data = [[1,2,3],[4,5,6]] df = pd.DataFrame(data,columns=('one','two','three')) print("Before deletion\n", df) del df['one'] print("After deletion\n", df)
Output
Before deletion one two three 0 1 2 3 1 4 5 6 After deletion two three 0 2 3 1 5 6
Solution 2
Define a dataframe
Delete a particular column using pop function. It is defined below
df.pop('one')
Example
import pandas as pd data = [[1,2,3],[4,5,6]] df = pd.DataFrame(data,columns=('one','two','three')) print("Before deletion\n", df) df.pop('one') print("After deletion\n", df)
Output
Before deletion one two three 0 1 2 3 1 4 5 6 After deletion two three 0 2 3 1 5 6
Solution 3
Define a dataframe
Apply the below method to drop more than one columns,
df.drop(columns=['one','two'],inplace = True)
Example
import pandas as pd data = [[1,2,3],[4,5,6]] df = pd.DataFrame(data,columns=('one','two','three')) print("Before deletion\n ", df) df.drop(columns=['one','two'],inplace = True) print("After deleting two columns\n", df)
Output
Before deletion one two three 0 1 2 3 1 4 5 6 After deletion two three 0 2 3 1 5 6 After deleting two columns three 0 3 1 6
- Related Articles
- Change Data Type for one or more columns in Pandas Dataframe
- Change Data Type for one or more columns in Pandas Dataframe
- Write a program in Python to remove first duplicate rows in a given dataframe
- Write a program in Python to transpose the index and columns in a given DataFrame
- Write a Python code to select any one random row from a given DataFrame
- Write a Python program to reshape a given dataframe in different ways
- How to select all columns except one in a Pandas DataFrame?
- Write a program in Python to convert a given dataframe to a LaTex document
- Python Pandas – Remove leading and trailing whitespace from more than one column
- Write a program in Python to localize Asian timezone for a given dataframe
- How to create a data frame with one or more columns as a list in R?
- Write a program in Python to split the date column into day, month, year in multiple columns of a given dataframe
- Write a program in Python to compute grouped data covariance and calculate grouped data covariance between two columns in a given dataframe
- Can we declare more than one class in a single Java program?\n
- Write a Python program to sort a given DataFrame by name column in descending order

Advertisements