ramlicious Blogs by Tina & Prabhu

April 1, 2018

Comparing Dataframes with Python

Filed under: Python — Prabhuram @ 9:11 pm
I created a "class" that will help compare two dataframes in Python. This is a easy-to-use version compared to the one that was created earlier.
Calling the functions in this class is easy.

Compare.compare(df[3:8], df[3:4])
import pandas as pd

class Compare(object):
    #e.g., compare(df[3:8], df[3:4])
    @staticmethod
    def compare(x, y):
        return pd.merge(x, y, on = list(x), how = 'outer', indicator = '_difference_')

    #e.g Compare.readxl("c:\\users\\vprab\downloads\\Financial Sample.xlsx")[3:8]
    @staticmethod
    def readxl(file, sheet_name = 0, header = 0, skiprows = 0):
        return pd.read_excel(file, sheet_name = sheet_name, header = header, skiprows = skiprows)
    
    #e.g., readcboard(sep = ',')
    @staticmethod
    def readcboard(sep = ','):
        return pd.read_clipboard(sep = sep)
    
    #e.g., writexl(pd.read_clipboard(sep='a'), "prp.xlsx")
    @staticmethod
    def writexl(df, file):
        writer = pd.ExcelWriter(file)
        df.to_excel(writer,'Sheet1',index=False)
        writer.save()
    
    #e.g., writecboard(pd.read_clipboard(sep='a'))
    @staticmethod
    def writecboard(df, excel = True):
        df.to_clipboard(excel = excel, index = False)

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URL

Sorry, the comment form is closed at this time.

Powered by WordPress