使用Python在两个Excel中找出丢失的事务

时间:2019-02-05 02:06:07

标签: python pandas

我有2个如下的excel csv文件

df1 =  {'Transaction_Name':['SC-001_Homepage', 'SC-002_Homepage', 'SC-003_Homepage', 'SC-001_Signinlink'], 'Count': [1, 0, 2, 1]}
df1 = pd.DataFrame(df1, columns=df1.keys())

df2 =  {'Transaction_Name':['SC-001_Homepage', 'SC-002_Homepage', 'SC-001_Signinlink', 'SC-002_Signinlink'], 'Count': [2, 1, 2, 1]}
df2 = pd.DataFrame(df2, columns=df2.keys())

df1中,我看到有一个名为SC-003_Homepage的额外事务,在df2中不存在。有人可以帮助我如何仅查找df2中缺少的交易吗?

到目前为止,我已经完成了以下工作来获取交易。

merged_df = pd.merge(df1, df2, on = 'Transaction_Name', suffixes=('_df1', '_df2'), how='inner')

2 个答案:

答案 0 :(得分:1)

也许一个简单的set就可以完成工作

set(df1['Transaction_Name']) - set(df2['Transaction_Name'])

答案 1 :(得分:1)

添加一个合并列,然后根据该列过滤丢失的数据。参见下面的示例。

有关更多信息,请参见merge documentation

import pandas as pd

df1 =  {'Transaction_Name':['SC-001_Homepage', 'SC-002_Homepage', 'SC-003_Homepage', 'SC-001_Signinlink'], 'Count': [1, 0, 2, 1]}
df1 = pd.DataFrame(df1, columns=df1.keys())

df2 =  {'Transaction_Name':['SC-001_Homepage', 'SC-002_Homepage', 'SC-001_Signinlink', 'SC-002_Signinlink'], 'Count': [2, 1, 2, 1]}
df2 = pd.DataFrame(df2, columns=df2.keys())

#create a merged df
merge_df = df1.merge(df2, on='Transaction_Name', how='outer', suffixes=['', '_'], indicator=True)

#filter rows which are missing in df2
missing_df2_rows = merge_df[merge_df['_merge'] =='left_only'][df1.columns]

#filter rows which are missing in df1
missing_df1_rows = merge_df[merge_df['_merge'] =='right_only'][df2.columns]

print missing_df2_rows
print missing_df1_rows

输出:

   Count Transaction_Name
2    2.0  SC-003_Homepage
   Count   Transaction_Name
4    NaN  SC-002_Signinlink