The Wayback Machine - https://web.archive.org/web/20240706132649/https://www.geeksforgeeks.org/python-pandas-dataframe-dropna/
Open In App

Pandas DataFrame.dropna() Method

Last Updated : 31 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Pandas is one of the packages that makes importing and analyzing data much easier. Sometimes CSV file has null values, which are later displayed as NaN in Pandas DataFrame. Pandas dropna() method allows the user to analyze and drop Rows/Columns with Null values in different ways. 

 Pandas DataFrame.dropna() Syntax

Syntax: DataFrameName.dropna(axis=0, how=’any’, thresh=None, subset=None, 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.
  • thresh: thresh takes integer value which tells minimum amount of na values to drop. 
  • subset: It’s an array which limits the dropping process to passed rows/columns through list. inplace: It is a boolean which makes the changes in data frame itself if True.

Pandas DataFrame dropna() Example

Example 1: Here we are using read_csv() to read our CSV file. Dropping Rows with at least 1 null value. A data frame is read and all rows with any Null values are dropped. The size of old and new data frames is compared to see how many rows had at least 1 Null value. 

Python3




# importing pandas module
import pandas as pd
 
# making data frame from csv file
data = pd.read_csv("nba.csv")
 
# making new data frame with dropped NA values
new_data = data.dropna(axis=0, how='any')
 
# comparing sizes of data frames
print("Old data frame length:", len(data),
      "\nNew data frame length:",
      len(new_data),
      "\nNumber of rows with at least 1 NA value: ",
      (len(data)-len(new_data)))


Output:

Since the difference is 94, there were 94 rows that had at least 1 Null value in any column.   

Old data frame length:  458 
New data frame length:  364 
Number of rows with at least 1 NA value:  94

Example 2: Changing axis and using how and inplace Parameters Two data frames are made. A column with all values = none is added to the new Data frame. Column names are verified to see if the Null column was inserted properly. Then Number of columns is compared before and after dropping NaN values.

Python3




# importing pandas module
import pandas as pd
 
# making data frame from csv file
data = pd.read_csv("nba.csv")
 
# making a copy of old data frame
new = pd.read_csv("nba.csv")
 
# creating a value with all null values in new data frame
new["Null Column"] = None
 
# checking if column is inserted properly
print(data.columns.values, "\n", new.columns.values)
 
# comparing values before dropping null column
print("\nColumn number before dropping Null column\n",
      len(data.dtypes), len(new.dtypes))
 
# dropping column with all null values
new.dropna(axis=1, how='all', inplace=True)
 
# comparing values after dropping null column
print("\nColumn number after dropping Null column\n",
      len(data.dtypes), len(new.dtypes))


Output:

['Name' 'Team' 'Number' 'Position' 'Age' 'Height' 'Weight' 'College'
 'Salary'] 
 ['Name' 'Team' 'Number' 'Position' 'Age' 'Height' 'Weight' 'College'
 'Salary' 'Null Column']

Column number before dropping Null column
 9 10

Column number after dropping Null column
 9 9


Previous Article
Next Article

Similar Reads

Python | Pandas Index.dropna()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.dropna() function return Index without NA/NaN values. All the missing values are removed and a new object is returned which
2 min read
Python | Pandas TimedeltaIndex.dropna
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.dropna() function drop all the missing values from the given TimedeltaIndex object. The function return Index with
2 min read
Python | Pandas Series.dropna()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.dropna() function return a new Series with missing values in the given series o
2 min read
Cleaning data with dropna in Pyspark
While dealing with a big size Dataframe which consists of many rows and columns they also consist of many NULL or None values at some row or column, or some of the rows are totally NULL or None. So in this case, if we apply an operation on the same Dataframe that contains many NULL or None values then we will not get the correct or desired output f
4 min read
Pandas DataFrame iterrows() Method | Pandas Method
Pandas DataFrame iterrows() iterates over a Pandas DataFrame rows in the form of (index, series) pair. This function iterates over the data frame column, it will return a tuple with the column name and content in the form of a series. Example: Python Code import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 32, 3
2 min read
Pandas DataFrame interpolate() Method | Pandas Method
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Python Pandas interpolate() method is used to fill NaN values in the DataFrame or Series using various interpolation techniques to fill the m
3 min read
Pandas DataFrame duplicated() Method | Pandas Method
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas duplicated() method identifies duplicated rows in a DataFrame. It returns a boolean series which is True only for unique rows. Ex
3 min read
Pandas DataFrame to_dict() Method | Convert DataFrame to Dictionary
Python is a great language for doing data analysis because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Pandas .to_dict() method is used to convert a DataFrame into a dictionary of series or list-like data type depending on the orient parameter. Exam
3 min read
Pandas DataFrame assign() Method | Create new Columns in DataFrame
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. The Dataframe.assign() method assigns new columns to a DataFrame, returning a new object (a copy) with the new columns added to the original one
2 min read
Pandas DataFrame hist() Method | Create Histogram in Pandas
A histogram is a graphical representation of the numerical data. Sometimes you'll want to share data insights with someone, and using graphical representations has become the industry standard. Pandas.DataFrame.hist() function plots the histogram of a given Data frame. It is useful in understanding the distribution of numeric variables. This functi
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg