
- 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
Python - Remove a column with all null values in Pandas
To remove a column with all null values, use the dropna() method and set the “how” parameter to “all” −
how='all'
At first, let us import the required libraries with their respective aliases −
import pandas as pd import numpy as np
Create a DataFrame. We have set the NaN values using the Numpy np.inf
dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],"Result": [np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN,np.NAN] } )
To remove a column with all null values, use dropna() and set the required parameters −
dataFrame.dropna(how='all', axis=1, inplace=True)
Example
Following is the code −
import numpy as np import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],"Result": [np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN,np.NAN] } ) print"DataFrame ...\n",dataFrame dataFrame.dropna(how='all', axis=1, inplace=True) print"\nDataFrame ...\n",dataFrame
Output
This will produce the following output −
DataFrame ... Result Student 0 NaN Jack 1 NaN Robin 2 NaN Ted 3 NaN Robin 4 NaN Scarlett 5 NaN Kat 6 NaN Ted DataFrame ... Student 0 Jack 1 Robin 2 Ted 3 Robin 4 Scarlett 5 Kat 6 Ted
- Related Articles
- Python Pandas - Filling missing column values with mode
- Python Pandas - Filling missing column values with median
- Python Pandas – Propagate non-null values backward
- Python Pandas – Propagate non-null values forward
- How to remove null values with PHP?
- Python - Remove duplicate values from a Pandas DataFrame
- Python Pandas – Check for Null values using notnull()
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Python Pandas – Remove numbers from string in a DataFrame column
- Update all the fields in a table with null or non-null values with MySQL
- Python Pandas - Get unique values from a column
- Python - Filter Rows Based on Column Values with query function in Pandas?
- MySQL query to remove Null Records in a column?
- Python Pandas – Display all the column names in a DataFrame
- Create a Pipeline and remove a column from DataFrame - Python Pandas

Advertisements