使用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.mzph.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…