Home > Software engineering >  implement shortcut for pandas code by extending it
implement shortcut for pandas code by extending it

Time:09-13

Pandas has a very nice feature to save a df to clipboard. This help a lot to take the df output and analyze/inspect further in excel.

 df={'A':['x','y','x','z','y'],
                   'B':[1,2,2,2,2],
                   'C':['a','b','a','d','d']}
df=pd.DataFrame(df)
df.to_clipboard(excel=True,index=False)

However I don't want to type df.to_clipboard(excel=True,index=False) each time I need to copy the df to clipboard. Is there a way I can do something like df.clip()

I tried to implement it like this but it does not work.

class ExtDF(pd.DataFrame):
    def __init__(self, *args, **kwargs):
        super(ExtDF,  self).__init__(*args, **kwargs)

    @property
    def _constructor(self):
        return ExtDF
    def clip(self):
        return self.to_clipboard(excel=True,index=False)

CodePudding user response:

I would monkey patch pandas.DataFrame rather than defining a subclass:

# define a function and monkey patch pandas.DataFrame
def clipxl(self):
    return self.to_clipboard(excel=True, index=False)

pd.DataFrame.clip = clipxl

# now let's try it
df={'A': ['x','y','x','z','y'],
    'B': [1,2,2,2,2],
    'C': ['a','b','a','d','d']}
df=pd.DataFrame(df)
df.clip()

clipboard content:

A   B   C
x   1   a
y   2   b
x   2   a
z   2   d
y   2   d

using a module

expand_pandas.py

import pandas as pd

# define a function and monkey patch pandas.DataFrame
def clipxl(self):
    return self.to_clipboard(excel=True,index=False)

def hello(self):
    return 'Hello World!'

pd.DataFrame.clip = clipxl
pd.DataFrame.hello = hello

In your environment:

import pandas as pd
import expand_pandas

df = pd.DataFrame({'A': ['x','y','x','z','y'],
                   'B': [1,2,2,2,2],
                   'C': ['a','b','a','d','d']})

df.hello()
# 'Hello World!'

df.clip()
# sends to clipboard

CodePudding user response:

Instead of the object oriented approach you could write a function that does it and pass the DataFrame as a parameter.

Alternatively, instead of modifying the DataFrame class itself, you can create a class that contains your dataframe and define a method that does what you want.

  • Related