Python:在Pandas數據框中查找缺失值

How to find Missing values in a data frame using Python/Pandas

如何使用Python / Pandas查找數據框中的缺失值

介紹: (Introduction:)

When you start working on any data science project the data you are provided is never clean. One of the most common issue with any data set are missing values. Most of the machine learning algorithms are not able to handle missing values. The missing values needs to be addressed before proceeding to applying any machine learning algorithm.

當您開始從事任何數據科學項目時,所提供的數據永遠不會干凈。 任何數據集最常見的問題之一就是缺少值。 大多數機器學習算法無法處理缺失值。 在繼續應用任何機器學習算法之前,需要解決缺少的值。

Missing values can be handled in different ways depending on if the missing values are continuous or categorical. In this section I will address how to find missing values. In the next article i will address on how to address the missing values.

根據缺失值是連續的還是分類的,可以用不同的方式來處理缺失值。 在本節中,我將介紹如何查找缺失值。 在下一篇文章中,我將介紹如何解決缺失值。

查找缺失值: (Finding Missing Values:)

For this exercise i will be using “listings.csv” data file from Seattle Airbnb data. The data can be found under this link : https://www.kaggle.com/airbnb/seattle?select=listings.csv

在本練習中,我將使用Seattle Airbnb數據中的“ listings.csv”數據文件。 可以在以下鏈接下找到數據: https : //www.kaggle.com/airbnb/seattle?select=listings.csv

Step 1: Load the data frame and study the structure of the data frame.

步驟1:加載數據框并研究數據框的結構。

First step is to load the file and look at the structure of the file. When you have a big dateset with high number of columns it is hard to look at each columns and study the types of columns.

第一步是加載文件并查看文件的結構。 如果日期集較大且列數很高,則很難查看每個列并研究列的類型。

To find out how many of the columns are categorical and numerical we can use pandas “dtypes” to get the different data types and you can use pandas “value_counts()” function to get count of each data type. Value_counts groups all the unique instances and gives the count of each of those instances.

要了解有多少列是分類列和數字列,我們可以使用pandas“ dtypes”來獲取不同的數據類型,還可以使用pandas“ value_counts()”函數來獲取每種數據類型的計數。 Value_counts對所有唯一實例進行分組,并給出每個實例的計數。

As you can see below we have 62 columns which are objects (categorical data), 17 columns which are of float data type and 13 columns which are of int data type.

如下所示,我們有62列是對象(分類數據),有17列是浮點數據類型,有13列是int數據類型。

Image for post

Step 2: Separate categorical and numerical columns in the data frame

步驟2:將數據框中的類別和數字列分開

The reason to separate the categorical and numerical columns in the data frame is the method of handling missing values are different between these two data type which i will walk through in the next section.

在數據框中分隔類別和數字列的原因是,這兩種數據類型之間處理缺失值的方法不同,我將在下一節中介紹這些方法。

The easiest way to achieve this step is through filtering out the columns from the original data frame by data type. By using “dtypes” function and equality operator you can get which columns are objects (categorical variable) and which are not.

實現此步驟的最簡單方法是按數據類型從原始數據幀中過濾出列。 通過使用“ dtypes”函數和相等運算符,您可以了解哪些列是對象(分類變量),哪些不是。

Image for post

To get the column names of the columns which satisfy the above conditions we can use “df.columns”. The below code gives column names which are objects and column names which are not objects.

要獲得滿足上述條件的列的列名,我們可以使用“ df.columns”。 下面的代碼給出了作為對象的列名和不是對象的列名。

As you can see below we separated the original data frame into 2 and assigned them new variables. One for for categorical variables and one for non-categorical variables.

如下所示,我們將原始數據幀分為2個并為其分配了新變量。 一種用于分類變量,另一種用于非分類變量。

Image for post

Step 3: Find the missing values

步驟3:找出遺漏的值

Finding the missing values is the same for both categorical and continuous variables. We will use “num_vars” which holds all the columns which are not object data type.

對于分類變量和連續變量,找到缺失值都是相同的。 我們將使用“ num_vars”來保存所有非對象數據類型的列。

df[num_vars] will give you all the columns in “num_vars” which consists of all the columns in the data frame which are not object data type.

df [num_vars]將為您提供“ num_vars”中的所有列,該列由數據框中的所有非對象數據類型的列組成。

Image for post

We can use pandas “isnull()” function to find out all the fields which have missing values. This will return True if a field has missing values and false if the field does not have missing values.

我們可以使用熊貓的“ isnull()”函數來找出所有缺少值的字段。 如果字段缺少值,則返回True,否則返回false。

Image for post

To get how many missing values are in each column we use sum() along with isnull() which is shown below. This will sum up all the True’s in each column from the step above.

為了獲得每列中有多少個缺失值,我們使用sum()以及isull() ,如下所示。 這將匯總上述步驟中每一列中的所有True。

Image for post

Its always good practice to sort the columns in descending order so you can see what are the columns with highest missing values. To do this we can use sort_values() function. By default this function will sort in ascending order. Since we want the columns with highest missing values first we want to set it to descending. You can do this by passing “ascending=False” paramter in sort_values().

始終最好的做法是按降序對列進行排序,以便您可以看到缺失值最高的列。 為此,我們可以使用sort_values()函數。 默認情況下,此功能將按升序排序。 因為我們首先要使缺失值最高的列,所以我們希望將其設置為降序。 您可以通過在sort_values()中傳遞“ ascending = False”參數來實現。

Image for post

The above give you the count of missing values in each column. To get % of missing values in each column you can divide by length of the data frame. You can “len(df)” which gives you the number of rows in the data frame.

上面給出了每一列中缺失值的計數。 要獲得每一列中丟失值的百分比,您可以除以數據幀的長度。 您可以“ len(df)”,它為您提供數據框中的行數。

As you can see below license column is missing 100% of the data and square_feet column is missing 97% of data.

如您所見,License列缺少100%的數據,square_feet列缺少97%的數據。

Image for post
Image for post

結論 (Conclusion)

The above article goes over on how to find missing values in the data frame using Python pandas library. Below are the steps

上面的文章介紹了如何使用Python pandas庫在數據框中查找缺失值。 以下是步驟

  1. Use isnull() function to identify the missing values in the data frame

    使用isnull()函數來識別數據框中的缺失值

  2. Use sum() functions to get sum of all missing values per column.

    使用sum()函數可獲取每列所有缺失值的總和。

  3. use sort_values(ascending=False) function to get columns with the missing values in descending order.

    使用sort_values(ascending = False)函數以降序獲取缺少值的列。

  4. Divide by len(df) to get % of missing values in each column.

    len(df)除以得到每一列中丟失值的%。

In this section we identified missing values, in the next we go over on how to handle these missing values.

在本節中,我們確定了缺失值,接下來,我們將繼續介紹如何處理這些缺失值。

翻譯自: https://medium.com/analytics-vidhya/python-finding-missing-values-in-a-data-frame-3030aaf0e4fd

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391612.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391612.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391612.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

監督學習-回歸分析

一、數學建模概述 監督學習:通過已有的訓練樣本進行訓練得到一個最優模型,再利用這個模型將所有的輸入映射為相應的輸出。監督學習根據輸出數據又分為回歸問題(regression)和分類問題(classfication)&#…

leetcode 54. 螺旋矩陣(遞歸)

給你一個 m 行 n 列的矩陣 matrix ,請按照 順時針螺旋順序 ,返回矩陣中的所有元素。 示例 1: 輸入:matrix [[1,2,3],[4,5,6],[7,8,9]] 輸出:[1,2,3,6,9,8,7,4,5] 示例 2: 輸入:matrix [[1,…

微服務架構技能

2019獨角獸企業重金招聘Python工程師標準>>> 微服務架構技能 博客分類: 架構 (StuQ 微服務技能圖譜) 2課程簡介 本課程分為基礎篇和高級篇兩部分,旨在通過完整的案例,呈現微服務的開發、測試、構建、部署、…

phpstorm 調試_PhpStorm中的多用戶調試

phpstorm 調試by Ray Naldo雷納爾多(Ray Naldo) PhpStorm中的多用戶調試 (Multi-User Debugging in PhpStorm) 使用Xdebug和DBGp代理 (Using Xdebug and DBGp Proxy) “Er, wait a minute… Don’t you just use xdebug.remote_connect_back which has been introduced since …

Tableau Desktop認證:為什么要關心以及如何通過

Woah, Tableau!哇,Tableau! By now, almost everyone’s heard of the data visualization software that brought visual analytics to the public. Its intuitive drag and drop interface makes connecting to data, creating graphs, and sharing d…

約束布局constraint-layout導入失敗的解決方案 - 轉

今天有同事用到了約束布局,但是導入我的工程出現錯誤 **提示錯誤: Could not find com.Android.support.constraint:constraint-layout:1.0.0-alpha3** 我網上查了一下資料,都說是因為我的androidStudio版本是最新的穩定版導入這個包就會報這…

算法復習:冒泡排序

思想:對于一個列表,每個數都是一個"氣泡 ",數字越大表示"越重 ",最重的氣泡移動到列表最后一位,冒泡排序后的結果就是“氣泡”按照它們的重量依次移動到列表中它們相應的位置。 算法:搜索整個列表…

leetcode 59. 螺旋矩陣 II(遞歸)

給你一個正整數 n ,生成一個包含 1 到 n2 所有元素,且元素按順時針順序螺旋排列的 n x n 正方形矩陣 matrix 。 示例 1: 輸入:n 3 輸出:[[1,2,3],[8,9,4],[7,6,5]] 解題思路 按層進行數字的填充,每一層…

前端基礎進階(七):函數與函數式編程

縱觀JavaScript中所有必須需要掌握的重點知識中,函數是我們在初學的時候最容易忽視的一個知識點。在學習的過程中,可能會有很多人、很多文章告訴你面向對象很重要,原型很重要,可是卻很少有人告訴你,面向對象中所有的重…

期權數據 獲取_我如何免費獲得期權數據

期權數據 獲取by Harry Sauers哈里紹爾斯(Harry Sauers) 我如何免費獲得期權數據 (How I get options data for free) 網頁抓取金融簡介 (An introduction to web scraping for finance) Ever wished you could access historical options data, but got blocked by a paywall…

顯示與刪除使用工具

右擊工具菜單欄中的空白處選擇自定義 在彈出的自定義菜單中選擇命令選項在選擇想要往里面添加工具的菜單,之后在選擇要添加的工具 若想要刪除工具欄中的某個工具,在打開自定義菜單后,按住鼠標左鍵拖動要刪除工具到空白處 例如 轉載于:https:/…

js值的拷貝和值的引用_到達P值的底部:直觀的解釋

js值的拷貝和值的引用介紹 (Introduction) Welcome to this lesson on calculating p-values.歡迎參加有關計算p值的課程。 Before we jump into how to calculate a p-value, it’s important to think about what the p-value is really for.在我們開始計算p值之前&#xff…

leetcode 115. 不同的子序列(dp)

給定一個字符串 s 和一個字符串 t ,計算在 s 的子序列中 t 出現的個數。 字符串的一個 子序列 是指,通過刪除一些(也可以不刪除)字符且不干擾剩余字符相對位置所組成的新字符串。(例如,“ACE” 是 “ABCDE…

監督學習-KNN最鄰近分類算法

分類(Classification)指的是從數據中選出已經分好類的訓練集,在該訓練集上運用數據挖掘分類的技術建立分類模型,從而對沒有分類的數據進行分類的分析方法。 分類問題的應用場景:用于將事物打上一個標簽,通常…

istio 和 kong_如何啟動和運行Istio

istio 和 kongby Chris Cooney克里斯庫尼(Chris Cooney) 如何啟動和運行Istio (How to get Istio up and running) 而一旦完成,您就可以做的瘋狂的事情。 (And the crazy stuff you can do once it is.) The moment you get Istio working on your cluster, it fee…

js練習--貪吃蛇(轉)

最近一直在看javascript,但是發現不了動力。就開始想找動力,于是在網上找到了一個用js寫的貪吃蛇游戲。奈何還不會用git,就只能先這樣保存著。哈哈哈,這也算第一篇博客了,以后會堅持用自己的代碼寫博客的,下…

bzoj千題計劃169:bzoj2463: [中山市選2009]誰能贏呢?

http://www.lydsy.com/JudgeOnline/problem.php?id2463 n為偶數時,一定可以被若干個1*2 矩形覆蓋 先手每次從矩形的一端走向另一端,后手每次走向一個新的矩形 所以先手必勝 n為奇數時,先手走完一步后,剩下同n為偶數 所以先手必敗…

無監督學習-主成分分析和聚類分析

聚類分析(cluster analysis)是將一組研究對象分為相對同質的群組(clusters)的統計分析技術,即將觀測對象的群體按照相似性和相異性進行不同群組的劃分,劃分后每個群組內部各對象相似度很高,而不…

struts實現分頁_在TensorFlow中實現點Struts

struts實現分頁If you want to get started on 3D Object Detection and more specifically on Point Pillars, I have a series of posts written on it just for that purpose. Here’s the link. Also, going through the Point Pillars paper directly will be really help…

封裝jQuery下載文件組件

使用jQuery導出文檔文件 jQuery添加download組件 jQuery.download function(url, data, method){if( url && data ){data typeof data string ? data : paramEdit(data);     function paramEdit(obj){        var temStr "",tempStr"…