Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
Drop Empty Columns in Pandas
Next article icon

Drop Empty Columns in Pandas

Last Updated : 17 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:


Next Article
Drop Empty Columns in Pandas

S

skrg141
Improve
Article Tags :
  • Python
  • Python-pandas
  • Python pandas-dataFrame
Practice Tags :
  • python

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
    Pandas DataFrame.columns
    In Pandas, DataFrame.columns attribute returns the column names of a DataFrame. It gives access to the column labels, returning an Index object with the column labels that may be used for viewing, modifying, or creating new column labels for a DataFrame.Note: This attribute doesn't require any param
    2 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences