在 Python 中自动进行 Winmerge 比较

Automating Winmerge comparison in Python

我需要一名助手编写 python 中两个 table 之间的比较,目前在 winmerge 中完成。

代码如下

import pandas as pd

上周的table

df1=pd.read_csv(r"C:\Users\ri0a\OneDrive - Department of Environment, Land, Water and Planning\Python practice\pvmodules+_210326.csv")

本周 table 有新型号和到期日期

df2=pd.read_csv(r"C:\Users\ri0a\OneDrive - Department of Environment, Land, Water and Planning\Python practice\pvmodules+_210401.csv")

table头像如下

第三栏是PV_module证书:有效期。我想设置一个类似excel logic '=IF (D2

下一步,导入dataframe_diff包

from dataframe_diff import dataframe_diff

执行差异

d1_column,d2_additional=dataframe_diff(df1,df2,key=['PV Module Certificate: Licensee/Certificate Holder Account','Model Number/s'])

使用此包 d2_additional 显示与上周相比,本周是否添加了与型号相关的新行。

但是,我正在尝试复制以下输出

涉及的任务有

  1. 如果某些模型(在本例中为一行)包含在上周的 table 中,但在本周的 table 中丢失,我想在中分配一个新字段“已过期”旁边的新列“状态”。/或仅从那些缺失的行中创建一个新数据框,d2_expires。
  2. 另一个数据框,其中上周丢失但本周添加的行或产品模型仍然存在...如 d2_additional。
  3. 第三个数据框,其中相同行(相同证书+相同型号但不同的新到期日期)的任何更改(例如到期日期)被捕获为d3_comparison。

请帮我解决这个问题。

提前致谢。

现在:与

一样
d2_expires = merged_df[merged_df._merge == 'left_only']

并与

d2_additional = merged_df[merged_df._merge == 'right_only']

我得到相同的输出。返回相同的行,但情况不应如此。 如下图所示

这个和加法一样

最后,我得到一个错误 d2_comaprison。

d3_comparison = merged_df[merged_df._merge == 'both'].\
        loc[lambda x: x.PV Module Certificate: Expiry Date_last_week != x.PV Module Certificate: Expiry Date_this_week]

很高兴与您分享我的数据。请发送电子邮件至 rsmnsu@gmail.com 以进行数据共享。

您必须确保在加载数据后将日期转换为日期时间格式,并将列重命名为更易于使用的名称(例如 'cert_holder'、'model_no'、'approval_date','expiry_date')

I want to set a logic similar to excel logic '=IF (D2<DATEVALUE("19/04/2021"),"Expired","OK). The objective here is to delete the entire rows where the expiry date is below a specific date/ today's date.

这(删除)可以通过以下方式完成:

df = df[df['expiry_date'] >= pd.Timestamp('today')]

# Or

df = df[df['expiry_date'] >= pd.Timestamp('2021-04-23')]

但这只适用于您的到期日期为日期时间格式的情况。

接下来合并两个数据帧:

merged_df = pd.merge(df1,df2, how='outer', on=['cert_holder','model_no'],\
                     suffixes=['_last_week','_this_week'], indicator=True)

If some model, in this case a row, was included in the last week's table, but is missing in current week's table, I want to assign a new field "Expired" in a new column "Status" next to it./ Or create a new dataframe, d2_expires, from only those missing rows.

d2_expires = merged_df[merged_df._merge == 'left_only']

Another dataframe, where the rows or the product models that were missing last week but added this week remains...As d2_additional.

d2_additional = merged_df[merged_df._merge == 'right_only']

A third dataframe, where any changes (for example expiry date) for same rows (same certificate + same model but different new expiry date) is captures as d3_comparison.

d3_comparison = merged_df[merged_df._merge == 'both'].\
        loc[lambda x: x.expiry_date_last_week != x.expiry_date_this_week]