Get the App
SLTechnology News&Howtos  ›  Development  › 

How to deal with Python missing values

Shulou Source: shulou.com Published: 2022-05-31 16:44:23 10月02日 Update

This article mainly introduces the relevant knowledge of how to deal with Python missing values, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to deal with Python missing values. Let's take a look at it.

First, build a DataFrame with missing values, as follows:

Import pandas as pdimport numpy as npdata = pd.DataFrame ([[1rect np.nanmae3], [np.nan,5,np.nan]], columns = ['axiajiaoyuanjia]) print (data)

Can you see that? Np.nan means Nan value, which means null value.

There is a function in numpy that can be used to see null values. No, there are two functions, isnull () and isna ().

Let's try their effects separately:

Import pandas as pdimport numpy as npdata = pd.DataFrame ([[1rect np.nanmae3], [np.nan,5,np.nan]], columns = ['axiajiaoyuzhongjia']) data.isnull () data.isna ()

As you can see, the function of these two functions is to determine whether the data is null or not, and if so, return true, either false.

Usually, there are two ways to deal with null values, one is to delete null values, and the other is to fill it in. Let's start with the first one, delete null values, and we can delete null values with the function dropna (). Note that it deletes the entire line that contains null values. For example:

Import pandas as pdimport numpy as npdata = pd.DataFrame ([[1rect np.nanmae3], [np.nan,5,np.nan]], columns = ['axiajiajiaoyuanjia']) data.dropna ()

After using the drop function in the above example, there is nothing left!

We can set to delete when each row has more than 2 null values (less than 2 reservations), and then use the parameter thresh of dropna ().

There are many ways to supplement null values, such as mean supplement, median supplement, etc. We need to use the function fillna (). For example, we populate the data above with the mean

The code is as follows:

Import pandas as pdimport numpy as npdata = pd.DataFrame ([[1rect np.nanmae3], [np.nan,5,np.nan]], columns = ['axiajiaoyuzhongjia]) data.fillna (data.mean ())

The result of the code running is as follows, and you can see that the null values are filled with the mean of the corresponding column.

This is the end of the article on "how to deal with Python missing values". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to deal with Python missing values". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Tags: Function missing that is mean knowledge code content method article processing no above two median value function example parameter meaning simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS vpn MariaDB OPPO Reno Redmi