Как удалить строки с пустыми значениями pandas

от admin

Pandas Data Cleaning: Remove rows with empty data or missing values

A trivia task that for data cleaning or machine learning model preparation is to remove rows or columns that have empty data or missing values.

Example of removing rows.

Here are two quick ways to do it.

  1. Use dropna() that when you don’t care which rows, just drop them.

2. Use drop() with a list of row numbers. Sometimes you do care which rows. Keep a copy of row numbers that has NaN. So we define a custom empty_rows() helper function first.

If readability is really not what you concern, I also make a one liner function for you as below: (Remeber ‘Readability counts’ from Zen? I would say it is ok for a quick helper function as long as you documented properly)

(if the dataset is hugh, runing in iterrows() loops may cause performance issue. You should consider subset to chunk then proceed and merge end results)

Then we use drop() to take list of row number to remove. We keep a copy of the list, in case we want to audit it later or manually alter it. For example, some rows are still valuable even they were missing some data. (Always case by case, depends on your business logic)

A more ‘pandas’ way is to to subset based on that condition, then get index list of that subset. .e.g

This approach utilizes numpy vectorization feature. It actually performs much faster than above manual iteration. Recommended.

And another one-liner to subset any row(s) contain missing / empty value:

Drop rows that with particular colums contain missing value:

It will remove rows whose ‘costPrice’ and/or ‘SuggessedPrice’ missing.

Dropping is quick, always bear in mind how to drop, whether to drop, should I drop.

Unnecessry dropping may impact your ultimate model test data and outcome.

pandas.DataFrame.dropna#

See the User Guide for more on which values are considered missing, and how to work with missing data.

Parameters axis <0 or ‘index’, 1 or ‘columns’>, default 0

Determine if rows or columns which contain missing values are removed.

0, or ‘index’ : Drop rows which contain missing values.

1, or ‘columns’ : Drop columns which contain missing value.

Changed in version 1.0.0: Pass tuple or list to drop on multiple axes. Only a single axis is allowed.

Determine if row or column is removed from DataFrame, when we have at least one NA or all NA.

Drop rows containing empty cells from a pandas DataFrame

I have a pd.DataFrame that was created by parsing some excel spreadsheets. A column of which has empty cells. For example, below is the output for the frequency of that column, 32320 records have missing values for Tenant.

I am trying to drop rows where Tenant is missing, however .isnull() option does not recognize the missing values.

The column has data type «Object». What is happening in this case? How can I drop records where Tenant is missing?

Amrita Sawant's user avatar

8 Answers 8

Pandas will recognise a value as null if it is a np.nan object, which will print as NaN in the DataFrame. Your missing values are probably empty strings, which Pandas doesn’t recognise as null. To fix this, you can convert the empty stings (or whatever is in your empty cells) to np.nan objects using replace() , and then call dropna() on your DataFrame to delete rows with null tenants.

To demonstrate, we create a DataFrame with some random values and some empty strings in a Tenants column:

Pandas Dropna – How to drop missing values?

In reality, majority of the datasets collected contain missing values due to manual errors, unavailability of information, etc. Although there are different ways for handling missing values, sometimes you have no other option but to drop those rows from the dataset. A common method for dropping rows and columns is using the pandas `dropna` function.

In this article, you will learn about the different functionalities of this method for dropping rows with missing values followed by some practical tips for using pandas dropna method.

Creating a Basic DataFrame

Creating Basic Dataframe

The pandas dropna function

Похожие статьи