使用Pandas 1.1.0進行穩健的2個DataFrames驗證

Pandas is one of the most used Python library for both data scientist and data engineers. Today, I want to share some Python tips to help us do qualification checks between 2 Dataframes.

Pandas是數據科學家和數據工程師最常用的Python庫之一。 今天,我想分享一些Python技巧,以幫助我們在2個數據框之間進行資格檢查。

Notice, I have used the word: qualification, instead of identical. Identical is easy to check, but qualification is a loose check. It is based on business logic. Therefore, it is harder to implement.

注意,我使用了單詞: qualification ,而不是完全相同。 相同很容易檢查,但資格是一個寬松的檢查。 它基于業務邏輯。 因此,很難實現。

不重新發明輪子 (Not reinvent the wheel)

In version 1.1.0 — released on July 28 2020, 8 days before — Pandas introduced the build-in compare function. All our following steps are built based on it.

在2020年7月28日發布的版本1.1.0(比之前晚8天)中,Pandas引入了內置比較功能 。 我們下面的所有步驟均基于此構建。

Image for post
Official Document ScreenShot for compare
官方文檔屏幕截圖進行比較

Tips: if you were using Anacondas distribution, you should use the following command line to upgrade your Pandas version.

提示 :如果使用的是Anacondas發行版,則應使用以下命令行升級Pandas版本。

低掛水果 (Low hanging fruit)

Always check number of columns between 2 frames first. In cases, this simple check could spot issues.

請務必先檢查2幀之間的列數。 在某些情況下,此簡單檢查可能會發現問題。

In certain scenarios, such as enrichment change, we could have a different number of columns. The definition of qualification could be: for all the former columns having the same value between 2 data frames. Therefore, we will identify columns to check and save them in variable Columns for later usage.

在某些情況下,例如富集變化,我們可以有不同數量的列。 資格的定義可以是:對于所有先前的列,在2個數據幀之間具有相同的值。 因此,我們將確定要檢查的列并將其保存在變量Columns中,以備后用。

解鎖鑰匙 (Keys to unlock)

In real application, we would have various ids to identify a record, such as user-id, order_id etc. In order to make a unique query, we may need to use a combination of these keys. Ultimately, we want to verify records with the same keys have the same column values.

在實際應用中,我們將使用各種ID來標識一條記錄,例如user-id,order_id等。為了進行唯一查詢,我們可能需要使用這些鍵的組合。 最終,我們要驗證具有相同鍵的記錄具有相同的列值。

The first step is to compose the key combination. This is where the DataFrame apply shines. We could use df.apply(lambda: x: func(x), axis = 1) to make any data transformation. With axis = 1, we are telling Pandas to do the same operation row by row. (axis = 0, column by column)

第一步是組合鍵。 這是DataFrame 應用的亮點。 我們可以使用df.apply(lambda:x:func(x), axis = 1 )進行任何數據轉換。 當axis = 1時 ,我們告訴Pandas 逐行執行相同的操作。 (軸= 0,逐列)

處理ValueError (Handle the ValueError)

For the new DataFrame.compare function, the following error is the most confusing. Let me try to explain.

對于新的DataFrame.compare函數,以下錯誤最令人困惑。 讓我嘗試解釋一下。

ValueError: Can only compare identically-labeled DataFrame objects

ValueError :只能比較標記相同的DataFrame對象

The reason for this error, is the shape and the order of columns between two data frames is not identical. Yes. DataFrame.compare works only for identical checking, not qualification checking.

此錯誤的原因是兩個數據幀之間的列的形狀和順序不相同。 是。 DataFrame.compare僅適用于相同檢查,而不適用于資格檢查。

The way to solve the issue is: use the keyColumn created before, compare for a subset between the DataFrames with the same keyColumn value. And do that for each keyColumn value.

解決該問題的方法是:使用之前創建的keyColumn ,比較具有相同keyColumn值的DataFrame之間的子集。 并對每個keyColumn值執行此操作。

If the dimensions for keyColumn from 2 DataFrames are different, raise the issue and skip the check.

如果來自2個數據幀的keyColumn的尺寸不同,請提出問題并跳過檢查。

帶走: (Take Away:)

Use latest Pandas 1.1.0 DataFrame.compare to do robust DataFrame qualification checks. In order to deal with ValueError, we use keyColumn to do multiple sub DataFrame checks and return the final decision.

使用最新的Pandas 1.1.0 DataFrame.compare進行可靠的DataFrame資格檢查。 為了處理ValueError,我們使用keyColumn進行多個子DataFrame檢查并返回最終決定。

翻譯自: https://towardsdatascience.com/robust-2-dataframes-verification-with-pandas-1-1-0-af22f328e622

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

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

相關文章

Maya開發

Maya開發(一)-- 緒論 (翻譯自Maya官方文檔)2008-05-09 15:33 緒論 Autodesk Maya 是一個開放的產品,就是說任何Autodesk以外的人都可以改變Maya現有的特征,或者 增加新的特性.你可以用兩個方法來修改MAYA: ME…

織夢在線報名平臺php,DedeCMSv5

DedeCMS v5國內專業的PHP網站內容管理系統-織夢內容管理系統v5.8 Roadmap狀態 ? 已完成 🔨 進行中 ? 未完成項目開發可以到織夢開發問題管理中進行交流反饋。🔨 調整DedeCMS目錄結構,將原有include中外部訪問的內容遷移出去;&am…

pom.xml文件詳解

<project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd "> <!-- 父項目的坐…

軟件工程第一次作業

&#xff08;1&#xff09;回想一下你初入大學時對計算機專業的暢想 當初你是如何做出選擇計算機專業的決定的&#xff1f; 當初選擇計算機專業是因為之前看大佬們參加信息競賽&#xff0c;覺得很厲害、很有意思&#xff0c;而且也希望能自己做一款游戲出來&#xff0c;所以就選…

置信區間的置信區間_什么是置信區間,為什么人們使用它們?

置信區間的置信區間I’m going to try something a little different today, in which I combine two (completely unrelated) topics I love talking about, and hopefully create something that is interesting and educational.今天&#xff0c;我將嘗試一些與眾不同的東西…

事實上著就是MAYA4.5完全手冊插件篇的內容

不過著好象侵權了&#xff0c;因為&#xff21;&#xff2c;&#xff29;&#xff21;&#xff33;聲明不得一任何方式傳播該手冊的部分或全部_炙墨追零 Maya不為插件提供二進制兼容性。每當發布新版本時&#xff0c;舊插件的源代碼要重新編譯。然而&#xff0c;我們的目標是保…

制作alipay-sdk-java包到本地倉庫

項目要用到支付寶的掃碼支付&#xff0c;后臺使用的maven 問了客服 官方目前沒有 maven 的地址只能手動安裝到本地了&#xff0c;如果建了maven 服務器也可以上傳到服務器上 從支付寶官網上下載sdk 制作本地安裝包 alipay-sdk-java.jar 放到D: 盤根目錄下 執行命令&#xff1a…

php中wlog是什么意思,d-log模式是什么意思

D-Log是一種高動態范圍的視頻素材記錄格式&#xff0c;總而言之這個色彩模式為后期調色提供了更大的空間。在相機和攝影機拍攝時&#xff0c;一顆高性能的傳感器通常支持11檔以上的動態范圍&#xff0c;而在8bit的照片或視頻上&#xff0c;以符合人眼感知的Gamma進行機內處理和…

PowerShell入門(三):如何快速地掌握PowerShell?

如何快速地掌握PowerShell呢&#xff1f;總的來說&#xff0c;就是要盡可能多的使用它&#xff0c;就像那句諺語說的&#xff1a;Practice makes perfect。當然這里還有一些原則和方法讓我們可以遵循。 有效利用交互式環境 一般來說&#xff0c;PowerShell有兩個主要的運行環境…

pca 主成分分析_通過主成分分析(PCA)了解您的數據并發現潛在模式

pca 主成分分析Save time, resources and stay healthy with data exploration that goes beyond means, distributions and correlations: Leverage PCA to see through the surface of variables. It saves time and resources, because it uncovers data issues before an h…

宏觀數據提取

http://219.235.129.58/fbk/checkSession.jsp http://219.235.129.58/indicatorYear.do?methodgetDczdsXml&bgqbDm1 大列表 http://219.235.129.58/indicatorYear.do?methodgetDczdsXml&bgqbDm1&FBZD_FJD0300&timeTypeyear 第二層 http://219.235.129.58/…

UML-- plantUML安裝

plantUML安裝 因為基于intellid idea,所以第一步自行安裝.setting->plugins 搜索plantUML安裝完成后&#xff0c;重啟idea 會有如下顯示安裝Graphviz 下載地址 https://graphviz.gitlab.io/_pages/Download/Download_windows.html配置Graphviz環境變量&#xff1a; dot -ver…

php不發送referer,php – 注意:未定義的索引:HTTP_REFERER

注意&#xff1a;未定義的索引&#xff1a;HTTP_REFERER$http_referer $_SERVER [‘HTTP_REFERER’]我使用這個從教程,它看起來不錯代碼也從包含文件中調用它我該怎么改&#xff1f;我添加了print_r($_ SERVER);現在頁面給了我這個Array ([UNIQUE_ID] > UoSxWa56310AAAwUck…

HDU 最大報銷額 (0 1 背包)

最大報銷額 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 29 Accepted Submission(s) : 11 Problem Description現有一筆經費可以報銷一定額度的發票。允許報銷的發票類型包括買圖書&#xff08;A類&#xff09…

rstudio 關聯r_使用關聯規則提出建議(R編程)

rstudio 關聯r背景 (Background) Retailers typically have a wealth of customer transaction data which consists of the type of items purchased by a customer, their value and the date they were purchased. Unless the retailer has a loyalty rewards system, they …

PHP進程1608占用了9012,swoole (ERRNO 9012): worker exit timeout, forced to terminate

swoole server下使用了swoole_event_add&#xff0c;在關閉服務的時候日志中出現了提示swWorker_reactor_is_empty (ERRNO 9012): worker exit timeout, forced to terminate并且關閉服務時間比正常情況下要慢。解決方法開啟 reload_async > true 配置注冊onWorderExit回調&…

C#高級應用之CodeDomProvider引擎篇 .

using System; using System.Text; using System.CodeDom.Compiler; using System.Reflection; using Microsoft.CSharp; namespace ToolPackages.CodeDomProvider { public class SampleCodeDomProvider { //代碼生成器對象 private static System.CodeDom.Compiler.Code…

linux—命令匯總

pwd # 顯示當前工作目錄cd /tmp # cd切換工作目錄pwdcd ./dir01 # .當前工作目錄cd ../dir02 # ..上層工作目錄cd - # -前一個工作目錄cd ~ …

flex 添加右鍵鏈接

private var myMenu:ContextMenu;private function setViewerVersion():void{var menuItem:ContextMenuItem new ContextMenuItem("技術支持&#xff1a;中科天宇軟件有限公司", true, true);menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, functio…

jquery數據折疊_通過位折疊縮小大數據

jquery數據折疊Sometimes your dataset is just too large, and you need a way to shrink it down to a reasonable size. I am suffering through this right now as I work on different machine learning techniques for checkers. I could work for over 18 years and buy…