Skip to content
-
Drop Empty Columns in Pandas
Last Updated :
17 Mar, 2025
Cleaning data is an essential step in data analysis. In this guide we will explore different ways to drop empty, null and zero-value columns in a Pandas DataFrame using Python. By the end you'll know how to efficiently clean your dataset using the dropna()
and replace()
methods.
Understanding dropna()
The dropna()
function is a powerful method in Pandas that allows us to remove rows or columns containing missing values (NaN
). Depending on the parameters used it can remove rows or columns where at least one value is missing or only those where all values are missing.
Syntax: DataFrameName.dropna(axis=0, how='any', inplace=False)
Parameters:
- axis: axis takes int or string value for rows/columns. Input can be 0 or 1 for Integer and ‘index’ or ‘columns’ for String.
- how: how takes string value of two kinds only (‘any’ or ‘all’). ‘any’ drops the row/column if ANY value is Null and ‘all’ drops only if ALL values are null.
- inplace: It is a boolean which makes the changes in the data frame itself if True.
Create a Sample DataFrame:
This is the sample data frame on which we will use to perform different operations.
Python
import numpy as np
import pandas as pd
df = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
df['Department'] = np.nan
print(df)
Output:

Example 1: Remove All Null Value Columns
This method removes columns where all values are NaN
. If a column is completely empty (contains only NaN
values) it is unnecessary for analysis and can be removed using dropna(how='all', axis=1)
.
Python
import numpy as np
import pandas as pd
df = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
df['Department'] = np.nan
display(df)
df.dropna(how='all', axis=1, inplace=True)
display(df)
Output:

Example 2: Replace Empty Strings with Null and Drop Null Columns
If a column contains empty strings we need to replace them with NaN
before dropping the column. Empty strings are not automatically recognized as missing values in Pandas so converting them to NaN
ensures they can be handled correctly. After conversion we use dropna(how='all', axis=1)
to remove columns that are entirely empty.
Python
import numpy as np
import pandas as pd
df = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
df['Department'] = np.nan
display(df)
nan_value = float("NaN")
df.replace("", nan_value, inplace=True)
df.dropna(how='all', axis=1, inplace=True)
display(df)
Output:

Example 3: Replace Zeros with Null and Drop Null Columns
If columns contain only zero values, we convert them to NaN
before dropping them.
Python
import numpy as np
import pandas as pd
df = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
df['Department'] = np.nan
display(df)
nan_value = float("NaN")
df.replace(0, nan_value, inplace=True)
df.dropna(how='all', axis=1, inplace=True)
display(df)
Output:

Example 4: Replace Both Zeros and Empty Strings with Null and Drop Null Columns
To clean a dataset fully we may need to replace both zeros and empty strings.
Python
import numpy as np
import pandas as pd
df = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
df['Department'] = np.nan
display(df)
nan_value = float("NaN")
df.replace(0, nan_value, inplace=True)
df.replace("", nan_value, inplace=True)
df.dropna(how='all', axis=1, inplace=True)
display(df)
Output:
Similar Reads
Pandas Drop Column When working with large datasets, there are often columns that are irrelevant or redundant. Pandas provides an efficient way to remove these unnecessary columns using the `drop()` function. In this article, we will cover various methods to drop columns from a DataFrame.Pythonimport pandas as pd data
4 min read
How to Drop Index Column in Pandas? When working with Pandas DataFrames, it's common to reset or remove custom indexing, especially after filtering or modifying rows. Dropping the index is useful when:We no longer need a custom index.We want to restore default integer indexing (0, 1, 2, ...).We're preparing data for exports or transfo
2 min read
Drop Duplicates Ignoring One Column-Pandas Pandas provide various features for users to implement on datasets. One such feature is dropping the duplicate rows, which can be done using the drop_duplicates function available in Pandas. There are some cases where the user wants to eliminate the duplicates but does not consider any certain colum
5 min read
How to Exclude Columns in Pandas? Excluding columns in a Pandas DataFrame is a common operation when you want to work with only relevant data. In this article, we will discuss various methods to exclude columns from a DataFrame, including using .loc[], .drop(), and other techniques.Exclude One Column using .loc[]We can exclude a col
2 min read
Create empty dataframe in Pandas The Pandas Dataframe is a structure that has data in the 2D format and labels with it. DataFrames are widely used in data science, machine learning, and other such places. DataFrames are the same as SQL tables or Excel sheets but these are faster in use.Empty DataFrame could be created with the help
1 min read
How to rename columns in Pandas DataFrame In this article, we will see how to rename column in Pandas DataFrame. The simplest way to rename columns in a Pandas DataFrame is to use the rename() function. This method allows renaming specific columns by passing a dictionary, where keys are the old column names and values are the new column nam
4 min read
`;
$(commentSectionTemplate).insertBefore(".article--recommended");
}
loadComments();
});
});
function loadComments() {
if ($("iframe[id*='discuss-iframe']").length top_of_element && top_of_screen articleRecommendedTop && top_of_screen articleRecommendedBottom)) {
if (!isfollowingApiCall) {
isfollowingApiCall = true;
setTimeout(function(){
if (loginData && loginData.isLoggedIn) {
if (loginData.userName !== $('#followAuthor').val()) {
is_following();
} else {
$('.profileCard-profile-picture').css('background-color', '#E7E7E7');
}
} else {
$('.follow-btn').removeClass('hideIt');
}
}, 3000);
}
}
});
}
$(".accordion-header").click(function() {
var arrowIcon = $(this).find('.bottom-arrow-icon');
arrowIcon.toggleClass('rotate180');
});
});
window.isReportArticle = false;
function report_article(){
if (!loginData || !loginData.isLoggedIn) {
const loginModalButton = $('.login-modal-btn')
if (loginModalButton.length) {
loginModalButton.click();
}
return;
}
if(!window.isReportArticle){
//to add loader
$('.report-loader').addClass('spinner');
jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', {
PRACTICE_API_URL: practiceAPIURL,
PRACTICE_URL:practiceURL
},function(responseTxt, statusTxt, xhr){
if(statusTxt == "error"){
alert("Error: " + xhr.status + ": " + xhr.statusText);
}
});
}else{
window.scrollTo({ top: 0, behavior: 'smooth' });
$("#report_modal_content").show();
}
}
function closeShareModal() {
const shareOption = document.querySelector('[data-gfg-action="share-article"]');
shareOption.classList.remove("hover_share_menu");
let shareModal = document.querySelector(".hover__share-modal-container");
shareModal && shareModal.remove();
}
function openShareModal() {
closeShareModal(); // Remove existing modal if any
let shareModal = document.querySelector(".three_dot_dropdown_share");
shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" }));
document.querySelector(".hover__share-modal-container").append(
Object.assign(document.createElement('div'), { className: "share__modal" }),
);
document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" }));
const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"];
socialOptions.forEach((socialOption) => {
const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" });
const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` });
const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` });
const shareLink = (socialOption === "Copy Link") ?
Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) :
Object.assign(document.createElement('a'), { className: "link-container" });
if (socialOption === "LinkedIn") {
shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`);
shareLink.setAttribute('target', '_blank');
}
if (socialOption === "WhatsApp") {
shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`);
shareLink.setAttribute('target', "_blank");
}
if (socialOption === "Twitter") {
shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`);
shareLink.setAttribute('target', "_blank");
}
shareLink.append(icon, socialText);
socialContainer.append(shareLink);
document.querySelector(".share__modal").appendChild(socialContainer);
//adding copy url functionality
if(socialOption === "Copy Link") {
shareLink.addEventListener("click", function() {
var tempInput = document.createElement("input");
tempInput.value = window.location.href;
document.body.appendChild(tempInput);
tempInput.select();
tempInput.setSelectionRange(0, 99999); // For mobile devices
document.execCommand('copy');
document.body.removeChild(tempInput);
this.querySelector(".share__option-text").textContent = "Copied"
})
}
});
// document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu"));
}
function toggleLikeElementVisibility(selector, show) {
document.querySelector(`.${selector}`).style.display = show ? "block" : "none";
}
function closeKebabMenu(){
document.getElementById("myDropdown").classList.toggle("show");
}