Python script to compare and merge excel files

Here I’m talking about a cool Python module using that you can compare two sheets in an excel sheet for a common value and merge the sheets.
First you need to install the module “pandas” for the solution. Python script is as below :


import pandas as pd

source1_df = pd.read_excel('a.xlsx', sheetname='Sheet1')
 source2_df = pd.read_excel('a.xlsx', sheetname='Sheet2')
 joined_df = pd.merge(source1_df,source2_df,on='Serial',how='outer')
 joined_df.to_excel('result.xlsx')




Note : replace the sheet1 and sheet2 with actual name of the pages and use the correct primary key name(Here “Serial”; case sensitive)


Eg :
sheet1 as below


Serial Hostname
123 abc
234 bcd
345 cde


Sheet2 as below


Serial Model
234 Sparc
345 IBM
567 HP


After executing the python script, output comes  as below :


  Serial Hostname Model
  0   123 abc  
1   234 bcd Sparc
     2   345 cde IBM
3   567   HP


Enjoy :)

Comments

Popular posts from this blog

Clear Screen in PUTTY (Linux commandline)

Python script to check ssh login on multiple hosts against a common password