Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding out missing transactions in two excels by using Python

Tags:

python

pandas

I have 2 excel csv files as below

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())

In df1 I could see that there is one extra transaction called SC-003_Homepage which is not there in df2. Can someone help me how to find only that transaction which is missing in df2?

So far I had done below work to get the transactions.

merged_df = pd.merge(df1, df2, on = 'Transaction_Name', suffixes=('_df1', '_df2'), how='inner')
like image 216
SG131712 Avatar asked May 29 '26 05:05

SG131712


2 Answers

Maybe a simple set will do the job

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

like image 142
Jose Angel Sanchez Avatar answered May 30 '26 17:05

Jose Angel Sanchez


Add a merger column and then filter the missing data based on that. see below example.

For more information see 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

Output:

   Count Transaction_Name
2    2.0  SC-003_Homepage
   Count   Transaction_Name
4    NaN  SC-002_Signinlink
like image 42
Chandella07 Avatar answered May 30 '26 19:05

Chandella07



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!